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