func TestManifestSet(t *testing.T) { mid := sous.ManifestID{ Source: sous.SourceLocation{ Repo: project1.Repo, }, } baseState := makeTestState() mani, present := baseState.Manifests.Get(mid) require.True(t, present) mani.Flavor = "vanilla" yml, err := yaml.Marshal(mani) require.NoError(t, err) in := bytes.NewBuffer(yml) state := makeTestState() dummyWriter := sous.DummyStateManager{State: state} writer := graph.StateWriter{StateWriter: &dummyWriter} sms := &SousManifestSet{ TargetManifestID: graph.TargetManifestID(mid), State: state, InReader: graph.InReader(in), StateWriter: writer, } assert.Equal(t, 0, dummyWriter.WriteCount) res := sms.Execute([]string{}) assert.Equal(t, 0, res.ExitCode()) assert.Equal(t, 1, dummyWriter.WriteCount) upManifest, present := state.Manifests.Get(mid) require.True(t, present) assert.Equal(t, upManifest.Flavor, "vanilla") }
func prepareCommand(t *testing.T, cl []string) (*CLI, *cmdr.PreparedExecution, fmt.Stringer, fmt.Stringer) { require := require.New(t) stdin := &bytes.Buffer{} stdout := &bytes.Buffer{} stderr := &bytes.Buffer{} s := &Sous{Version: semv.MustParse(`1.2.3`)} c, err := NewSousCLI(s, stdin, stdout, stderr) require.NoError(err) exe, err := c.Prepare(cl) require.NoError(err) return c, exe, stdout, stderr }
func assertSourceContextSuccess(t *testing.T, expected sous.ManifestID, flags *sous.ResolveFilter, ctx *sous.SourceContext) { disco := &SourceContextDiscovery{SourceContext: ctx} actual, err := newTargetManifestID(flags, disco) require.NoError(t, err) assert.Equal(t, actual.Source.Repo, expected.Source.Repo, "repos differ") assert.Equal(t, actual.Source.Dir, expected.Source.Dir, "offsets differ") assert.Equal(t, actual.Flavor, expected.Flavor, "flavors differ") }
func TestGitPushes(t *testing.T) { require := require.New(t) gsm, dsm := setupManagers(t) expected, err := gsm.ReadState() require.NoError(err) expected.Manifests.Add(&sous.Manifest{Source: sous.SourceLocation{Repo: "github.com/opentable/brandnew"}}) require.NoError(gsm.WriteState(expected)) expected, err = gsm.ReadState() require.NoError(err) runScript(t, `git reset --hard`, `testdata/origin`) //in order to reflect the change actual, err := dsm.ReadState() require.NoError(err) sameYAML(t, actual, expected) }
func TestHandlesManifestPut(t *testing.T) { assert := assert.New(t) require := require.New(t) q, err := url.ParseQuery("repo=gh") require.NoError(err) state := sous.NewState() state.Manifests.Add(&sous.Manifest{ Source: sous.SourceLocation{Repo: "gh"}, Kind: sous.ManifestKindService, }) writer := graph.StateWriter{StateWriter: &sous.DummyStateManager{State: state}} manifest := &sous.Manifest{ Source: sous.SourceLocation{Repo: "gh"}, Owners: []string{"sam", "judson"}, Kind: sous.ManifestKindService, } buf := &bytes.Buffer{} enc := json.NewEncoder(buf) enc.Encode(manifest) req, err := http.NewRequest("PUT", "", buf) require.NoError(err) th := &PUTManifestHandler{ Request: req, StateWriter: writer, State: state, QueryValues: &QueryValues{q}, } data, status := th.Exchange() assert.Equal(status, 200) require.IsType(&sous.Manifest{}, data) assert.Len(data.(*sous.Manifest).Owners, 2) assert.Equal(data.(*sous.Manifest).Owners[1], "judson") changed, found := state.Manifests.Get(sous.ManifestID{Source: sous.SourceLocation{Repo: "gh"}}) require.True(found) assert.Len(changed.Owners, 2) assert.Equal(changed.Owners[1], "judson") }
func runCommand(t *testing.T, args []string, dff config.DeployFilterFlags) string { out := &bytes.Buffer{} state := makeTestState() shc := sous.SourceHostChooser{} rf, err := dff.BuildFilter(shc.ParseSourceLocation) require.NoError(t, err) deps, err := state.Deployments() require.NoError(t, err) smg := SousMetadataGet{ DeployFilterFlags: dff, ResolveFilter: rf, State: state, CurrentGDM: graph.CurrentGDM{Deployments: deps}, OutWriter: graph.OutWriter(out), } res := smg.Execute(args) assert.Equal(t, 0, res.ExitCode()) return out.String() }
func TestGitReadState(t *testing.T) { require := require.New(t) gsm := NewGitStateManager(NewDiskStateManager("testdata/in")) actual, err := gsm.ReadState() require.NoError(err) expected := exampleState() sameYAML(t, actual, expected) }
func TestGitPulls(t *testing.T) { require := require.New(t) gsm, dsm := setupManagers(t) actual, err := gsm.ReadState() require.NoError(err) expected := exampleState() sameYAML(t, actual, expected) expected.Manifests.Add(&sous.Manifest{Source: sous.SourceLocation{Repo: "github.com/opentable/brandnew"}}) dsm.WriteState(expected) expected, err = dsm.ReadState() require.NoError(err) runScript(t, `git add . git commit -m ""`, `testdata/origin`) actual, err = gsm.ReadState() require.NoError(err) sameYAML(t, actual, expected) }
func sameYAML(t *testing.T, actual *sous.State, expected *sous.State) { assert := assert.New(t) require := require.New(t) actualManifests := actual.Manifests.Snapshot() expectedManifests := expected.Manifests.Snapshot() assert.Len(actualManifests, len(expectedManifests)) for mid, manifest := range expectedManifests { actual := *actualManifests[mid] assert.Contains(actualManifests, mid) if !assert.Equal(actual, *manifest) { _, differences := actual.Diff(manifest) t.Logf("DIFFERENCES (%q): %#v", mid, differences) } } actualYAML, err := yaml.Marshal(actual) require.NoError(err) expectedYAML, err := yaml.Marshal(expected) require.NoError(err) assert.Equal(actualYAML, expectedYAML) }
func TestHandlesManifestGetNotKnown(t *testing.T) { assert := assert.New(t) require := require.New(t) q, err := url.ParseQuery("repo=gh") require.NoError(err) th := &GETManifestHandler{ State: sous.NewState(), QueryValues: &QueryValues{q}, } _, status := th.Exchange() assert.Equal(404, status) }
func TestBuildDeployRequest(t *testing.T) { assert := assert.New(t) require := require.New(t) di := "dockerImage" rID := "reqID" env := sous.Env{"test": "yes"} rez := sous.Resources{"cpus": "0.1"} vols := sous.Volumes{&sous.Volume{}} dr, err := buildDeployRequest(di, env, rez, rID, vols) require.NoError(err) assert.NotNil(dr) assert.Equal(dr.Deploy.RequestId, rID) }
func TestInvokeWithUnknownFlags(t *testing.T) { log.SetFlags(log.Flags() | log.Lshortfile) assert := assert.New(t) require := require.New(t) stdin := &bytes.Buffer{} stdout := &bytes.Buffer{} stderr := &bytes.Buffer{} s := &Sous{Version: semv.MustParse(`1.2.3`)} c, err := NewSousCLI(s, stdin, stdout, stderr) require.NoError(err) c.Invoke([]string{`sous`, `-cobblers`}) assert.Regexp(`flag provided but not defined`, stderr.String()) }
func TestHandlesManifestGet(t *testing.T) { assert := assert.New(t) require := require.New(t) q, err := url.ParseQuery("repo=gh") require.NoError(err) state := sous.NewState() state.Manifests.Add(&sous.Manifest{Source: sous.SourceLocation{Repo: "gh"}}) th := &GETManifestHandler{ State: state, QueryValues: &QueryValues{q}, } _, status := th.Exchange() assert.Equal(status, 200) }
func TestGitWriteState(t *testing.T) { require := require.New(t) s := exampleState() if err := os.RemoveAll("testdata/out"); err != nil { t.Fatal(err) } gsm := NewGitStateManager(NewDiskStateManager("testdata/out")) require.NoError(gsm.WriteState(s)) d := exec.Command("diff", "-r", "testdata/in", "testdata/out") out, err := d.CombinedOutput() if err != nil { t.Log("Output not as expected:") t.Log(string(out)) t.Fatal("") } }
func TestRealDiffConcentration(t *testing.T) { log.SetFlags(log.Flags() | log.Lshortfile) assert := assert.New(t) require := require.New(t) intended := NewDeployments() existing := NewDeployments() defs := Defs{Clusters: map[string]*Cluster{"test": &Cluster{}}} makeDepl := func(repo, verstr string, num int) *Deployment { version := semv.MustParse(verstr) cl := defs.Clusters["test"] owners := OwnerSet{} owners.Add("judson") return &Deployment{ SourceID: SourceID{ Location: SourceLocation{ Repo: repo, }, Version: version, }, Cluster: cl, ClusterName: "test", DeployConfig: DeployConfig{ NumInstances: num, Env: map[string]string{}, Resources: map[string]string{ "cpu": ".1", "memory": "100", "ports": "1", }, }, Owners: owners, } } repoOne := "github.com/opentable/one" repoTwo := "github.com/opentable/two" repoThree := "github.com/opentable/three" repoFour := "github.com/opentable/four" repoFive := "github.com/opentable/five" existing.MustAdd(makeDepl(repoOne, "111.1.1", 1)) //remove //intended: gone existing.MustAdd(makeDepl(repoTwo, "1.0.0", 1)) //same intended.MustAdd(makeDepl(repoTwo, "1.0.0", 1)) //same // existing: doesn't yet intended.MustAdd(makeDepl(repoFour, "1.0.0", 1)) //create existing.MustAdd(makeDepl(repoThree, "1.0.0", 1)) //changed intended.MustAdd(makeDepl(repoThree, "1.0.0", 2)) //changed existing.MustAdd(makeDepl(repoFive, "1.0.0", 1)) //changed intended.MustAdd(makeDepl(repoFive, "2.0.0", 1)) //changed dc := existing.Diff(intended).Concentrate(defs) ds, err := dc.collect() require.NoError(err) if assert.Len(ds.Gone.Snapshot(), 1, "Should have one deleted item.") { it, _ := ds.Gone.Any(func(*Manifest) bool { return true }) assert.Equal(string(it.Source.Repo), repoOne) } if assert.Len(ds.Same.Snapshot(), 1, "Should have one unchanged item.") { it, _ := ds.Same.Any(func(*Manifest) bool { return true }) assert.Equal(string(it.Source.Repo), repoTwo) } if assert.Len(ds.Changed, 2, "Should have two modified items.") { chNum, chVer := ds.Changed[0], ds.Changed[1] if repoThree == chVer.name.Source.Repo { chNum, chVer = chVer, chNum } assert.Equal(repoThree, string(chNum.name.Source.Repo)) assert.Equal(repoThree, string(chNum.Prior.Source.Repo)) assert.Equal(repoThree, string(chNum.Post.Source.Repo)) log.Printf("%+v", chNum) log.Printf("%+v", chNum.Prior) log.Printf("%+v", chNum.Post) assert.Equal(chNum.Prior.Deployments["test"].NumInstances, 1) assert.Equal(chNum.Post.Deployments["test"].NumInstances, 2) assert.Equal(repoFive, string(chVer.name.Source.Repo)) assert.Equal(repoFive, string(chVer.Prior.Source.Repo)) assert.Equal(repoFive, string(chVer.Post.Source.Repo)) ver1 := semv.MustParse("1.0.0") ver2 := semv.MustParse("2.0.0") assert.Equal(ver1, chVer.Prior.Deployments["test"].Version) assert.Equal(ver2, chVer.Post.Deployments["test"].Version) } if assert.Equal(ds.New.Len(), 1, "Should have one added item.") { it, _ := ds.New.Any(func(*Manifest) bool { return true }) assert.Equal(string(it.Source.Repo), repoFour) } }