Beispiel #1
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 #2
0
func (t *PlaybookTest) Run() {
	playbook := neat.NewPlaybook()
	for _, module := range t.Modules {
		playbook.Add(module)
	}

	stats, err := playbook.Play()
	if t.Err != "" {
		if err == nil {
			t.Fatalf("expected error, got nil")
		} else if t.Err != err.Error() {
			t.Fatalf("expected error '%s', got '%s'", t.Err, err.Error())
		}
	} else if err != nil {
		t.Fatalf("error running playbook: %s", err)
	}

	if stats.Ok != t.Expected.Ok {
		t.Errorf("ok: expected %d, got %d", t.Expected.Ok, stats.Ok)
	}
	if stats.Changed != t.Expected.Changed {
		t.Errorf("changed: expected %d, got %d", t.Expected.Changed, stats.Changed)
	}
	if stats.Deferred != t.Expected.Deferred {
		t.Errorf("deferred: expected %d, got %d", t.Expected.Deferred, stats.Deferred)
	}
	if stats.Skipped != t.Expected.Skipped {
		t.Errorf("skipped: expected %d, got %d", t.Expected.Skipped, stats.Skipped)
	}
}
Beispiel #3
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 #4
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 #5
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 #6
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 #7
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)
	}
}