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

     1  package apt
     2  
     3  /*
     4  var runAptTests = false
     5  
     6  func init() {
     7  	c := exec.Command("which", "apt-cache")
     8  	c.Run()
     9  	runAptTests = c.ProcessState.Success()
    10  }
    11  
    12  func TestAptDryRun(t *testing.T) {
    13  	if !runAptTests {
    14  		return
    15  	}
    16  
    17  	res, err := goansible.RunAdhocTask("apt", "pkg=acct dryrun=true")
    18  	if err != nil {
    19  		panic(err)
    20  	}
    21  
    22  	if !res.Changed {
    23  		t.Error("No change detected")
    24  	}
    25  
    26  	if res.Data.Get("installed") != "" {
    27  		t.Error("incorrectly found an installed version")
    28  	}
    29  
    30  	if res.Data.Get("candidate") == "" {
    31  		t.Error("no candidate found")
    32  	}
    33  
    34  	if res.Data.Get("dryrun") != true {
    35  		t.Error("dryrun not true")
    36  	}
    37  }
    38  
    39  func removeAcct() {
    40  	exec.Command("apt-get", "remove", "-y", "--force-yes", "acct").CombinedOutput()
    41  }
    42  
    43  func TestAptInstallAndRemoves(t *testing.T) {
    44  	if !runAptTests {
    45  		return
    46  	}
    47  
    48  	defer removeAcct()
    49  
    50  	res, err := goansible.RunAdhocTask("apt", "pkg=acct")
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  
    55  	if !res.Changed {
    56  		t.Fatal("No change detected")
    57  	}
    58  
    59  	grep := fmt.Sprintf(`apt-cache policy acct | grep "Installed: %s"`,
    60  		res.Data.Get("installed"))
    61  
    62  	_, err = exec.Command("sh", "-c", grep).CombinedOutput()
    63  
    64  	if err != nil {
    65  		t.Errorf("package did not install")
    66  	}
    67  
    68  	// Test that it skips too
    69  	// Do this here instead of another test because installing is slow
    70  
    71  	res2, err := goansible.RunAdhocTask("apt", "pkg=acct")
    72  	if err != nil {
    73  		panic(err)
    74  	}
    75  
    76  	if res2.Changed {
    77  		t.Fatal("acct was reinstalled incorrectly")
    78  	}
    79  
    80  	res3, err := goansible.RunAdhocTask("apt", "pkg=acct state=absent")
    81  	if err != nil {
    82  		panic(err)
    83  	}
    84  
    85  	if !res3.Changed {
    86  		t.Fatal("acct was not removed")
    87  	}
    88  
    89  	if res3.Data.Get("removed") != res.Data.Get("installed") {
    90  		t.Fatalf("removed isn't set to the version removed: '%s '%s'",
    91  			res3.Data.Get("removed"), res.Data.Get("installed"))
    92  	}
    93  
    94  	res4, err := goansible.RunAdhocTask("apt", "pkg=acct state=absent")
    95  	if err != nil {
    96  		panic(err)
    97  	}
    98  
    99  	if res4.Changed {
   100  		t.Fatal("acct was removed again")
   101  	}
   102  
   103  }
   104  
   105  */