Skip to content

Commit

Permalink
Test mods and simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
philrenaud committed Sep 10, 2024
1 parent 2c92244 commit 655d509
Showing 1 changed file with 74 additions and 92 deletions.
166 changes: 74 additions & 92 deletions command/job_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ namespace "default" {
}
}

func blocksFromOutput(t *testing.T, out string) []string {
t.Helper()
rawBlocks := strings.Split(out, "Version")
// trim empty blocks from whitespace in the output
var blocks []string
for _, block := range rawBlocks {
trimmed := strings.TrimSpace(block)
if trimmed != "" {
blocks = append(blocks, trimmed)
}
}
return blocks
}

func TestJobHistoryCommand_Diffs(t *testing.T) {
ci.Parallel(t)

Expand All @@ -176,33 +190,33 @@ func TestJobHistoryCommand_Diffs(t *testing.T) {
state := srv.Agent.Server().State()

// Create a job with multiple versions
job := mock.Job()
v0 := mock.Job()

job.ID = "test-job-history"
job.TaskGroups[0].Count = 1
must.NoError(t, state.UpsertJob(structs.MsgTypeTestSetup, 1000, nil, job))
v0.ID = "test-job-history"
v0.TaskGroups[0].Count = 1
must.NoError(t, state.UpsertJob(structs.MsgTypeTestSetup, 1000, nil, v0))

v2 := job.Copy()
v2.TaskGroups[0].Count = 2
v2.TaggedVersion = &structs.JobTaggedVersion{
v1 := v0.Copy()
v1.TaskGroups[0].Count = 2
v1.TaggedVersion = &structs.JobTaggedVersion{
Name: "example-tag",
Description: "example-description",
}
must.NoError(t, state.UpsertJob(structs.MsgTypeTestSetup, 1001, nil, v2))
must.NoError(t, state.UpsertJob(structs.MsgTypeTestSetup, 1001, nil, v1))

v3 := job.Copy()
v3.TaskGroups[0].Count = 3
must.NoError(t, state.UpsertJob(structs.MsgTypeTestSetup, 1002, nil, v3))
v2 := v0.Copy()
v2.TaskGroups[0].Count = 3
must.NoError(t, state.UpsertJob(structs.MsgTypeTestSetup, 1002, nil, v2))

v4 := job.Copy()
v4.TaskGroups[0].Count = 4
must.NoError(t, state.UpsertJob(structs.MsgTypeTestSetup, 1003, nil, v4))
v3 := v0.Copy()
v3.TaskGroups[0].Count = 4
must.NoError(t, state.UpsertJob(structs.MsgTypeTestSetup, 1003, nil, v3))

t.Run("Without diffs", func(t *testing.T) {
ui := cli.NewMockUi()
cmd := &JobHistoryCommand{Meta: Meta{Ui: ui}}

code := cmd.Run([]string{"-address", url, job.ID})
code := cmd.Run([]string{"-address", url, v0.ID})
must.Zero(t, code)

out := ui.OutputWriter.String()
Expand All @@ -214,42 +228,34 @@ func TestJobHistoryCommand_Diffs(t *testing.T) {
ui := cli.NewMockUi()
cmd := &JobHistoryCommand{Meta: Meta{Ui: ui}}

code := cmd.Run([]string{"-p", "-address", url, job.ID})
code := cmd.Run([]string{"-p", "-address", url, v0.ID})
must.Zero(t, code)

out := ui.OutputWriter.String()
rawBlocks := strings.Split(out, "Version")
// trim empty blocks from whitespace in the output
var blocks []string
for _, block := range rawBlocks {
trimmed := strings.TrimSpace(block)
if trimmed != "" {
blocks = append(blocks, trimmed)
}
}
blocks := blocksFromOutput(t, out)

// Check that we have 4 versions
must.Eq(t, 4, len(blocks))
must.Len(t, 4, blocks)
must.Eq(t, 4, strings.Count(out, "Version"))
must.Eq(t, 3, strings.Count(out, "Diff"))

// Diffs show up for all versions except the first one
must.True(t, strings.Contains(blocks[0], "Diff"))
must.True(t, strings.Contains(blocks[1], "Diff"))
must.True(t, strings.Contains(blocks[2], "Diff"))
must.False(t, strings.Contains(blocks[3], "Diff"))
must.StrContains(t, blocks[0], "Diff")
must.StrContains(t, blocks[1], "Diff")
must.StrContains(t, blocks[2], "Diff")
must.StrNotContains(t, blocks[3], "Diff")

// Check that the diffs are specifically against their predecessor
must.True(t, strings.Contains(blocks[0], "\"3\" => \"4\""))
must.True(t, strings.Contains(blocks[1], "\"2\" => \"3\""))
must.True(t, strings.Contains(blocks[2], "\"1\" => \"2\""))
must.StrContains(t, blocks[0], "\"3\" => \"4\"")
must.StrContains(t, blocks[1], "\"2\" => \"3\"")
must.StrContains(t, blocks[2], "\"1\" => \"2\"")
})

t.Run("With diffs against a specific version that doesnt exist", func(t *testing.T) {
ui := cli.NewMockUi()
cmd := &JobHistoryCommand{Meta: Meta{Ui: ui}}

code := cmd.Run([]string{"-p", "-diff-version", "4", "-address", url, job.ID})
code := cmd.Run([]string{"-p", "-diff-version", "4", "-address", url, v0.ID})
must.One(t, code)
// Error that version 4 doesnt exists
must.StrContains(t, ui.ErrorWriter.String(), "version 4 not found")
Expand All @@ -259,35 +265,27 @@ func TestJobHistoryCommand_Diffs(t *testing.T) {
ui := cli.NewMockUi()
cmd := &JobHistoryCommand{Meta: Meta{Ui: ui}}

code := cmd.Run([]string{"-p", "-diff-version", "3", "-address", url, job.ID})
code := cmd.Run([]string{"-p", "-diff-version", "3", "-address", url, v0.ID})
must.Zero(t, code)

out := ui.OutputWriter.String()
rawBlocks := strings.Split(out, "Version")
// trim empty blocks from whitespace in the output
var blocks []string
for _, block := range rawBlocks {
trimmed := strings.TrimSpace(block)
if trimmed != "" {
blocks = append(blocks, trimmed)
}
}
blocks := blocksFromOutput(t, out)

// Check that we have 4 versions
must.Eq(t, 4, len(blocks))
must.Len(t, 4, blocks)
must.Eq(t, 4, strings.Count(out, "Version"))
must.Eq(t, 3, strings.Count(out, "Diff"))

// Diffs show up for all versions except the specified one
must.False(t, strings.Contains(blocks[0], "Diff"))
must.True(t, strings.Contains(blocks[1], "Diff"))
must.True(t, strings.Contains(blocks[2], "Diff"))
must.True(t, strings.Contains(blocks[3], "Diff"))
must.StrNotContains(t, blocks[0], "Diff")
must.StrContains(t, blocks[1], "Diff")
must.StrContains(t, blocks[2], "Diff")
must.StrContains(t, blocks[3], "Diff")

// Check that the diffs are specifically against the tagged version (which has a count of 2)
must.True(t, strings.Contains(blocks[1], "\"4\" => \"3\""))
must.True(t, strings.Contains(blocks[2], "\"4\" => \"2\""))
must.True(t, strings.Contains(blocks[3], "\"4\" => \"1\""))
// Check that the diffs are specifically against the tagged version (which has a count of 4)
must.StrContains(t, blocks[1], "\"4\" => \"3\"")
must.StrContains(t, blocks[2], "\"4\" => \"2\"")
must.StrContains(t, blocks[3], "\"4\" => \"1\"")

})

Expand All @@ -296,42 +294,34 @@ func TestJobHistoryCommand_Diffs(t *testing.T) {
cmd := &JobHistoryCommand{Meta: Meta{Ui: ui}}

// Diff against version 1 instead
code := cmd.Run([]string{"-p", "-diff-version", "2", "-address", url, job.ID})
code := cmd.Run([]string{"-p", "-diff-version", "2", "-address", url, v0.ID})
must.Zero(t, code)

out := ui.OutputWriter.String()
rawBlocks := strings.Split(out, "Version")
// trim empty blocks from whitespace in the output
var blocks []string
for _, block := range rawBlocks {
trimmed := strings.TrimSpace(block)
if trimmed != "" {
blocks = append(blocks, trimmed)
}
}
blocks := blocksFromOutput(t, out)

// Check that we have 4 versions
must.Eq(t, 4, len(blocks))
must.Len(t, 4, blocks)
must.Eq(t, 4, strings.Count(out, "Version"))
must.Eq(t, 3, strings.Count(out, "Diff"))

// Diffs show up for all versions except the specified one
must.True(t, strings.Contains(blocks[0], "Diff"))
must.False(t, strings.Contains(blocks[1], "Diff"))
must.True(t, strings.Contains(blocks[2], "Diff"))
must.True(t, strings.Contains(blocks[3], "Diff"))

// Check that the diffs are specifically against the tagged version (which has a count of 2)
must.True(t, strings.Contains(blocks[0], "\"3\" => \"4\""))
must.True(t, strings.Contains(blocks[2], "\"3\" => \"2\""))
must.True(t, strings.Contains(blocks[3], "\"3\" => \"1\""))
must.StrContains(t, blocks[0], "Diff")
must.StrNotContains(t, blocks[1], "Diff")
must.StrContains(t, blocks[2], "Diff")
must.StrContains(t, blocks[3], "Diff")

// Check that the diffs are specifically against the tagged version (which has a count of 3)
must.StrContains(t, blocks[0], "\"3\" => \"4\"")
must.StrContains(t, blocks[2], "\"3\" => \"2\"")
must.StrContains(t, blocks[3], "\"3\" => \"1\"")
})

t.Run("With diffs against a specific tag that doesnt exist", func(t *testing.T) {
ui := cli.NewMockUi()
cmd := &JobHistoryCommand{Meta: Meta{Ui: ui}}

code := cmd.Run([]string{"-p", "-diff-tag", "nonexistent-tag", "-address", url, job.ID})
code := cmd.Run([]string{"-p", "-diff-tag", "nonexistent-tag", "-address", url, v0.ID})
must.One(t, code)
must.StrContains(t, ui.ErrorWriter.String(), "tag \"nonexistent-tag\" not found")
})
Expand All @@ -341,34 +331,26 @@ func TestJobHistoryCommand_Diffs(t *testing.T) {

// Run history command with diff against the tag
cmd := &JobHistoryCommand{Meta: Meta{Ui: ui}}
code := cmd.Run([]string{"-p", "-diff-tag", "example-tag", "-address", url, job.ID})
code := cmd.Run([]string{"-p", "-diff-tag", "example-tag", "-address", url, v0.ID})
must.Zero(t, code)

out := ui.OutputWriter.String()
rawBlocks := strings.Split(out, "Version")
// trim empty blocks from whitespace in the output
var blocks []string
for _, block := range rawBlocks {
trimmed := strings.TrimSpace(block)
if trimmed != "" {
blocks = append(blocks, trimmed)
}
}
blocks := blocksFromOutput(t, out)

// Check that we have 4 versions
must.Eq(t, 4, len(blocks))
must.Len(t, 4, blocks)
must.Eq(t, 4, strings.Count(out, "Version"))
must.Eq(t, 3, strings.Count(out, "Diff"))

// Check that the diff is present for versions other than the tagged version
must.True(t, strings.Contains(blocks[0], "Diff"))
must.True(t, strings.Contains(blocks[1], "Diff"))
must.False(t, strings.Contains(blocks[2], "Diff"))
must.True(t, strings.Contains(blocks[3], "Diff"))
must.StrContains(t, blocks[0], "Diff")
must.StrContains(t, blocks[1], "Diff")
must.StrNotContains(t, blocks[2], "Diff")
must.StrContains(t, blocks[3], "Diff")

// Check that the diffs are specifically against the tagged version (which has a count of 2)
must.True(t, strings.Contains(blocks[0], "\"2\" => \"4\""))
must.True(t, strings.Contains(blocks[1], "\"2\" => \"3\""))
must.True(t, strings.Contains(blocks[3], "\"2\" => \"1\""))
must.StrContains(t, blocks[0], "\"2\" => \"4\"")
must.StrContains(t, blocks[1], "\"2\" => \"3\"")
must.StrContains(t, blocks[3], "\"2\" => \"1\"")
})
}

0 comments on commit 655d509

Please sign in to comment.