Beispiel #1
0
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())
	}
}
Beispiel #2
0
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")
	}
}
Beispiel #3
0
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)
	}
}
Beispiel #4
0
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
}
Beispiel #5
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)
	}
}
Beispiel #6
0
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)
	}
}
Beispiel #7
0
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)
	}
}
Beispiel #8
0
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)
	}
}
Beispiel #9
0
func TestModule_Execute_Create_WrongArgs(t *testing.T) {
	_, err := neat.CreateModule("execute")
	if err == nil {
		t.Fatal("expected an error, got nil")
	}
}