func TestModule_File_Create(t *testing.T) { tmpdir, err := ioutil.TempDir(os.TempDir(), "neat-test") if err != nil { t.Fatal(err) } defer os.RemoveAll(tmpdir) filepath := fmt.Sprintf("%s/test.txt", tmpdir) m, err := neat.CreateModule("file", map[string]interface{}{ "path": filepath, "mode": 0644, "state": "present", }) if err != nil { t.Fatal(err) } playbook := neat.NewPlaybook() _, status, err := m.Execute(playbook) if err != nil { t.Fatal(err) } if status != neat.ModuleChanged { t.Fatalf("expected status changed, got %s", status) } stat, err := os.Stat(filepath) if err != nil { t.Fatal(err) } else if stat.Mode() != 0644 { t.Errorf("expected file to be mode 0644, got %v", stat.Mode()) } }
func TestModule_File_Create_CheckMode(t *testing.T) { tmpdir, err := ioutil.TempDir(os.TempDir(), "neat-test") if err != nil { t.Fatal(err) } defer os.RemoveAll(tmpdir) filepath := fmt.Sprintf("%s/test.txt", tmpdir) m, err := neat.CreateModule("file", map[string]interface{}{ "path": filepath, "mode": 0644, "state": "present", }) if err != nil { t.Fatal(err) } playbook := neat.NewPlaybook() playbook.SetCheckMode(true) _, status, err := m.Execute(playbook) if err != nil { t.Fatal(err) } if status != neat.ModuleChanged { t.Fatalf("expected status changed, got %s", status) } _, err = os.Stat(filepath) if err == nil { t.Error("file present, but was not supposed to be created") } }
func TestModule_Execute_Create(t *testing.T) { tmpdir, err := ioutil.TempDir(os.TempDir(), "neat-test") if err != nil { t.Fatal(err) } defer os.RemoveAll(tmpdir) filepath := fmt.Sprintf("%s/test.txt", tmpdir) m, err := neat.CreateModule("execute", map[string]interface{}{ "command": fmt.Sprintf("touch %s", filepath), }) if err != nil { t.Fatal(err) } playbook := neat.NewPlaybook() _, status, err := m.Execute(playbook) if err != nil { t.Fatal(err) } if status != neat.ModuleChanged { t.Fatalf("execute status changed, got %s", status) } _, err = os.Stat(filepath) if err != nil { t.Fatal(err) } }
func realMain() int { m, err := neat.CreateModule(*flModule) if err != nil { fmt.Printf("unable to create module: %s\n", err.Error()) return 1 } playbook := neat.NewPlaybook() result, status, err := m.Execute(playbook) if err != nil { fmt.Printf("module failed: %s\n", err) } if result != nil { fmt.Printf("%s: ", status.String()) encoder := json.NewEncoder(os.Stdout) if err := encoder.Encode(result); err != nil { fmt.Println(err) return 1 } } else { fmt.Println(status.String()) } return 0 }
func TestModule_Service_Create(t *testing.T) { _, err := neat.CreateModule("service", map[string]interface{}{ "path": "neatd", "state": "restarted", }) if err != nil { t.Fatal(err) } }
func TestModule_Template_Create(t *testing.T) { _, err := neat.CreateModule("template", map[string]interface{}{ "src": "foobar.conf.tmpl", "dest": "/etc/foobar.conf", "mode": 0644, "state": "present", }) if err != nil { t.Fatal(err) } }
func TestModule_Execute_FailedCommand(t *testing.T) { m, err := neat.CreateModule("execute", map[string]interface{}{ "command": "false", }) if err != nil { t.Fatal(err) } playbook := neat.NewPlaybook() _, status, err := m.Execute(playbook) if err == nil { t.Fatal("expected an error, got nil") } else if status != neat.ModuleFailed { t.Fatalf("expected status failed, got %s", status) } }
func TestModule_File_Remove(t *testing.T) { tmpdir, err := ioutil.TempDir(os.TempDir(), "neat-test") if err != nil { t.Fatal(err) } defer os.RemoveAll(tmpdir) filepath := fmt.Sprintf("%s/test.txt", tmpdir) f, err := os.Create(filepath) if err != nil { t.Fatal(err) } f.Close() m, err := neat.CreateModule("file", map[string]interface{}{ "path": filepath, "state": "absent", }) if err != nil { t.Fatal(err) } playbook := neat.NewPlaybook() _, status, err := m.Execute(playbook) if err != nil { t.Fatal(err) } if status != neat.ModuleChanged { t.Fatalf("expected status changed, got %s", status) } _, err = os.Stat(filepath) if err == nil { t.Fatal("file present, but was supposed to be removed") } else if !os.IsNotExist(err) { t.Fatal(err) } }
func TestModule_Execute_Create_WrongArgs(t *testing.T) { _, err := neat.CreateModule("execute") if err == nil { t.Fatal("expected an error, got nil") } }