github.com/masahide/goansible@v0.0.0-20160116054156-01eac649e9f2/builtin_test.go (about)

     1  package goansible
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  )
    11  
    12  func inTmp(blk func()) {
    13  	dir, err := os.Getwd()
    14  	if err != nil {
    15  		panic(err)
    16  	}
    17  
    18  	tmpDir := filepath.Join("test", fmt.Sprintf("builtin-test-%d", os.Getpid()))
    19  	os.Mkdir(tmpDir, 0755)
    20  	os.Chdir(tmpDir)
    21  
    22  	defer os.RemoveAll(tmpDir)
    23  	defer os.Chdir(dir)
    24  
    25  	blk()
    26  }
    27  
    28  var testData = []byte("test")
    29  var testData2 = []byte("foobar")
    30  
    31  func TestCopySimple(t *testing.T) {
    32  	inTmp(func() {
    33  		ioutil.WriteFile("a.txt", testData, 0644)
    34  
    35  		res, err := RunAdhocTask("copy", "src=a.txt dest=b.txt")
    36  		if err != nil {
    37  			panic(err)
    38  		}
    39  
    40  		if !res.Changed {
    41  			t.Errorf("The copy didn't change anything")
    42  		}
    43  
    44  		data, err := ioutil.ReadFile("b.txt")
    45  		if err != nil {
    46  			panic(err)
    47  		}
    48  
    49  		if !bytes.Equal(testData, data) {
    50  			t.Errorf("The copy didn't move the righte bytes")
    51  		}
    52  	})
    53  }
    54  
    55  func TestCopyFailsOnMissingSrc(t *testing.T) {
    56  	inTmp(func() {
    57  		_, err := RunAdhocTask("copy", "src=a.txt dest=b.txt")
    58  		if err == nil {
    59  			t.Errorf("Copy did not fail")
    60  		}
    61  	})
    62  }
    63  
    64  func TestCopyShowsNoChangeWhenFilesTheSame(t *testing.T) {
    65  	inTmp(func() {
    66  		ioutil.WriteFile("a.txt", testData, 0644)
    67  		ioutil.WriteFile("b.txt", testData, 0644)
    68  
    69  		res, err := RunAdhocTask("copy", "src=a.txt dest=b.txt")
    70  		if err != nil {
    71  			panic(err)
    72  		}
    73  
    74  		if res.Changed {
    75  			t.Errorf("The copy changed something incorrectly")
    76  		}
    77  
    78  		if res.Data["md5sum"].Read().(string) == "" {
    79  			t.Errorf("md5sum not returned")
    80  		}
    81  	})
    82  }
    83  
    84  func TestCopyMakesFileInDir(t *testing.T) {
    85  	inTmp(func() {
    86  		ioutil.WriteFile("a.txt", testData, 0644)
    87  		os.Mkdir("b", 0755)
    88  
    89  		res, err := RunAdhocTask("copy", "src=a.txt dest=b")
    90  		if err != nil {
    91  			panic(err)
    92  		}
    93  
    94  		if !res.Changed {
    95  			t.Errorf("The copy didn't change anything")
    96  		}
    97  
    98  		data, err := ioutil.ReadFile("b/a.txt")
    99  		if err != nil {
   100  			panic(err)
   101  		}
   102  
   103  		if !bytes.Equal(testData, data) {
   104  			t.Errorf("The copy didn't move the righte bytes")
   105  		}
   106  	})
   107  }
   108  
   109  func TestCopyRemovesALink(t *testing.T) {
   110  	inTmp(func() {
   111  		ioutil.WriteFile("a.txt", testData, 0644)
   112  		ioutil.WriteFile("c.txt", testData2, 0644)
   113  
   114  		os.Symlink("c.txt", "b.txt")
   115  
   116  		res, err := RunAdhocTask("copy", "src=a.txt dest=b.txt")
   117  		if err != nil {
   118  			panic(err)
   119  		}
   120  
   121  		if !res.Changed {
   122  			t.Errorf("The copy didn't change anything")
   123  		}
   124  
   125  		stat, err := os.Stat("b.txt")
   126  
   127  		if !stat.Mode().IsRegular() {
   128  			t.Errorf("copy didn't remove the link")
   129  		}
   130  
   131  		data, err := ioutil.ReadFile("b.txt")
   132  		if err != nil {
   133  			panic(err)
   134  		}
   135  
   136  		if !bytes.Equal(testData, data) {
   137  			t.Errorf("The copy didn't move the righte bytes")
   138  		}
   139  
   140  		data, err = ioutil.ReadFile("c.txt")
   141  		if err != nil {
   142  			panic(err)
   143  		}
   144  
   145  		if !bytes.Equal(testData2, data) {
   146  			t.Errorf("c.txt was overriden improperly")
   147  		}
   148  	})
   149  }
   150  
   151  func TestCopyPreservesMode(t *testing.T) {
   152  	inTmp(func() {
   153  		ioutil.WriteFile("a.txt", testData, 0755)
   154  
   155  		_, err := RunAdhocTask("copy", "src=a.txt dest=b.txt")
   156  		if err != nil {
   157  			panic(err)
   158  		}
   159  
   160  		stat, err := os.Stat("b.txt")
   161  		if err != nil {
   162  			panic(err)
   163  		}
   164  
   165  		if stat.Mode().Perm() != 0755 {
   166  			t.Errorf("Copy didn't preserve the perms")
   167  		}
   168  	})
   169  }
   170  
   171  func TestCommand(t *testing.T) {
   172  	res, err := RunAdhocTask("command", "date")
   173  	if err != nil {
   174  		panic(err)
   175  	}
   176  
   177  	if !res.Changed {
   178  		t.Errorf("changed not properly set")
   179  	}
   180  
   181  	if res.Data["rc"].Read().(int) != 0 {
   182  		t.Errorf("return code not captured")
   183  	}
   184  
   185  	if res.Data["stdout"].Read().(string) == "" {
   186  		t.Errorf("stdout was not captured: '%s'", res.Data["stdout"].Read())
   187  	}
   188  }
   189  
   190  func TestShell(t *testing.T) {
   191  	res, err := RunAdhocTask("shell", "echo \"hello dear\"")
   192  	if err != nil {
   193  		panic(err)
   194  	}
   195  
   196  	if res.Data["rc"].Read().(int) != 0 {
   197  		t.Errorf("return code not captured")
   198  	}
   199  
   200  	if res.Data["stdout"].Read().(string) != "hello dear" {
   201  		t.Errorf("stdout was not captured: '%s'", res.Data["stdout"].Read())
   202  	}
   203  }
   204  
   205  func TestShellSeesNonZeroRC(t *testing.T) {
   206  	res, err := RunAdhocTask("shell", "exit 1")
   207  	if err == nil {
   208  		panic(err)
   209  	}
   210  
   211  	if res.Data["rc"].Read().(int) != 1 {
   212  		t.Errorf("return code not captured")
   213  	}
   214  
   215  	if res.Data["stdout"].Read().(string) != "" {
   216  		t.Errorf("stdout was not captured")
   217  	}
   218  }
   219  
   220  func TestScriptExecutesRelative(t *testing.T) {
   221  	res, err := RunAdhocTask("script", "test/test_script.sh")
   222  	if err != nil {
   223  		panic(err)
   224  	}
   225  
   226  	if res.Data["rc"].Read().(int) != 0 {
   227  		t.Errorf("return code not captured")
   228  	}
   229  
   230  	if res.Data["stdout"].Read().(string) != "hello script" {
   231  		t.Errorf("stdout was not captured")
   232  	}
   233  }