func TestNew(t *testing.T) { reporter, err := New(ReporterConfig{}, logging.DefaultLogger, podstatus.NewConsul(statusstoretest.NewFake(), kp.PreparerPodStatusNamespace)) if reporter != nil || err == nil { t.Errorf("Should have gotten a nil reporter and an error with empty config") } reporter, err = New(ReporterConfig{ SQLiteDatabasePath: "bar", EnvironmentExtractorPath: "/some/nonexistent/path", }, logging.DefaultLogger, podstatus.NewConsul(statusstoretest.NewFake(), kp.PreparerPodStatusNamespace)) if reporter != nil || err == nil { t.Errorf("Should have gotten a nil reporter when EnvironmentExtractorPath doesn't exist") } tempDir, err := ioutil.TempDir("", "podprocess_reporter_tests") if err != nil { t.Fatalf("Could not set up temp directory for tests: %s", err) } defer os.RemoveAll(tempDir) // os.Create() creates a file with mode 0666, which is not executable nonExecutableExtractor, err := os.Create(filepath.Join(tempDir, "non_executable_extractor")) if err != nil { t.Fatalf("Could not create non-executable extractor file") } reporter, err = New(ReporterConfig{ SQLiteDatabasePath: "foo", EnvironmentExtractorPath: nonExecutableExtractor.Name(), }, logging.DefaultLogger, podstatus.NewConsul(statusstoretest.NewFake(), kp.PreparerPodStatusNamespace)) if reporter != nil || err == nil { t.Errorf("Should have gotten a nil reporter with non-executable environemnt_extractor_path") } // now make it executable executableExtractor := nonExecutableExtractor err = executableExtractor.Chmod(0777) if err != nil { t.Fatalf("Could not make environment extractor path executable: %s", err) } reporter, err = New(ReporterConfig{ SQLiteDatabasePath: "foo", EnvironmentExtractorPath: executableExtractor.Name(), }, logging.DefaultLogger, podstatus.NewConsul(statusstoretest.NewFake(), kp.PreparerPodStatusNamespace)) if err != nil { t.Errorf("Unexpected error calling New(): %s", err) } }
// Instantiates a preparer with a podStore and podStatusStore backed by a fake // consul KV implementation. The podStore field is necessary for acquiring the // "intent" manifest for uuid pods, while the podStatusStore is necessary for // getting the reality manifest func testResultSetPreparer() *Preparer { fakeStatusStore := statusstoretest.NewFake() return &Preparer{ podStatusStore: podstatus.NewConsul(fakeStatusStore, kp.PreparerPodStatusNamespace), podStore: podstore.NewConsul(consulutil.NewFakeClient().KV()), } }
func startReporter(t *testing.T, tempDir string) (string, chan struct{}, podstatus.Store) { dbPath := filepath.Join(tempDir, "finishes.db") // The extractor doesn't need to do anything because we'll mimic its behavior in the test, // but it needs to exist and be executable in order to pass constructor validation extractor, err := os.Create(filepath.Join(tempDir, "env_extractor")) if err != nil { t.Fatalf("Could not create env extractor file: %s", err) } // make it executable err = extractor.Chmod(0777) if err != nil { t.Fatalf("could not make env extractor executable: %s", err) } config := ReporterConfig{ SQLiteDatabasePath: dbPath, // This setting only matters for generating the FinishExec in the preparer, // it won't affect the tests in this file EnvironmentExtractorPath: extractor.Name(), WorkspaceDirPath: tempDir, PollInterval: 1 * time.Millisecond, } store := podstatus.NewConsul(statusstoretest.NewFake(), kp.PreparerPodStatusNamespace) reporter, err := New(config, logging.DefaultLogger, store) if err != nil { t.Fatalf("Error creating reporter: %s", err) } quitCh := make(chan struct{}) err = reporter.Run(quitCh) if err != nil { t.Fatalf("Unable to start reporter: %s", err) } return dbPath, quitCh, store }
func setupServerWithFakePodStatusStore() (podstatus.Store, store) { fakePodStatusStore := podstatus.NewConsul(statusstoretest.NewFake(), kp.PreparerPodStatusNamespace) return fakePodStatusStore, store{ podStatusStore: fakePodStatusStore, } }
func newFixture() *consulStore { return &consulStore{ statusStore: statusstoretest.NewFake(), namespace: "test_namespace", } }