github.com/leg100/ots@v0.0.7-0.20210919080622-034055ced4bd/workspace_queue_test.go (about) 1 package ots 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 type mockPlanEnqueuer struct{} 10 11 func (u *mockPlanEnqueuer) EnqueuePlan(string) error { return nil } 12 13 func TestWorkspaceQueue_AddRun(t *testing.T) { 14 tests := []struct { 15 name string 16 active *Run 17 pending []*Run 18 run *Run 19 want func(*testing.T, *Run, []*Run) 20 }{ 21 { 22 name: "no existing runs", 23 run: &Run{ID: "run-123", ConfigurationVersion: &ConfigurationVersion{}}, 24 want: func(t *testing.T, active *Run, pending []*Run) { 25 if assert.NotNil(t, active) { 26 assert.Equal(t, "run-123", active.ID) 27 } 28 assert.Equal(t, 0, len(pending)) 29 }, 30 }, 31 { 32 name: "existing active run", 33 active: &Run{ID: "run-active"}, 34 run: &Run{ID: "run-123", ConfigurationVersion: &ConfigurationVersion{}}, 35 want: func(t *testing.T, active *Run, pending []*Run) { 36 if assert.NotNil(t, active) { 37 assert.Equal(t, "run-active", active.ID) 38 } 39 if assert.Equal(t, 1, len(pending)) { 40 assert.Equal(t, "run-123", pending[0].ID) 41 } 42 }, 43 }, 44 { 45 name: "existing active run and pending run", 46 active: &Run{ID: "run-active"}, 47 pending: []*Run{{ID: "run-pending"}}, 48 run: &Run{ID: "run-123", ConfigurationVersion: &ConfigurationVersion{}}, 49 want: func(t *testing.T, active *Run, pending []*Run) { 50 if assert.NotNil(t, active) { 51 assert.Equal(t, "run-active", active.ID) 52 } 53 if assert.Equal(t, 2, len(pending)) { 54 assert.Equal(t, "run-pending", pending[0].ID) 55 assert.Equal(t, "run-123", pending[1].ID) 56 } 57 }, 58 }, 59 { 60 name: "add speculative run", 61 run: &Run{ID: "run-123", ConfigurationVersion: &ConfigurationVersion{Speculative: true}}, 62 want: func(t *testing.T, active *Run, pending []*Run) { 63 assert.Nil(t, active) 64 assert.Equal(t, 0, len(pending)) 65 }, 66 }, 67 } 68 69 for _, tt := range tests { 70 t.Run(tt.name, func(t *testing.T) { 71 q := &WorkspaceQueue{ 72 PlanEnqueuer: &mockPlanEnqueuer{}, 73 Active: tt.active, 74 Pending: tt.pending, 75 } 76 77 q.Add(tt.run) 78 79 tt.want(t, q.Active, q.Pending) 80 }) 81 } 82 } 83 84 func TestWorkspaceQueue_RemoveRun(t *testing.T) { 85 tests := []struct { 86 name string 87 active *Run 88 pending []*Run 89 run *Run 90 want func(*testing.T, *Run, []*Run) 91 }{ 92 { 93 name: "remove active run", 94 active: &Run{ID: "run-123"}, 95 run: &Run{ID: "run-123", ConfigurationVersion: &ConfigurationVersion{}}, 96 want: func(t *testing.T, active *Run, pending []*Run) { 97 assert.Nil(t, active) 98 }, 99 }, 100 { 101 name: "remove active run, pending run takes its place", 102 active: &Run{ID: "run-123"}, 103 pending: []*Run{ 104 {ID: "run-pending-0"}, 105 {ID: "run-pending-1"}, 106 }, 107 run: &Run{ID: "run-123", ConfigurationVersion: &ConfigurationVersion{}}, 108 want: func(t *testing.T, active *Run, pending []*Run) { 109 if assert.NotNil(t, active) { 110 assert.Equal(t, "run-pending-0", active.ID) 111 } 112 if assert.Equal(t, 1, len(pending)) { 113 assert.Equal(t, "run-pending-1", pending[0].ID) 114 } 115 }, 116 }, 117 { 118 name: "remove only pending run", 119 active: &Run{ID: "run-active"}, 120 pending: []*Run{{ID: "run-123"}}, 121 run: &Run{ID: "run-123", ConfigurationVersion: &ConfigurationVersion{}}, 122 want: func(t *testing.T, active *Run, pending []*Run) { 123 assert.Equal(t, 0, len(pending)) 124 }, 125 }, 126 { 127 name: "remove pending run amongst other pending runs", 128 active: &Run{ID: "run-active"}, 129 pending: []*Run{ 130 {ID: "run-pending-0"}, 131 {ID: "run-123"}, 132 {ID: "run-pending-1"}, 133 }, 134 run: &Run{ID: "run-123", ConfigurationVersion: &ConfigurationVersion{}}, 135 want: func(t *testing.T, active *Run, pending []*Run) { 136 if assert.Equal(t, 2, len(pending)) { 137 assert.Equal(t, "run-pending-0", pending[0].ID) 138 assert.Equal(t, "run-pending-1", pending[1].ID) 139 } 140 }, 141 }, 142 { 143 name: "remove speculative run", 144 active: &Run{ID: "run-active"}, 145 pending: []*Run{{ID: "run-pending"}}, 146 run: &Run{ID: "run-123", ConfigurationVersion: &ConfigurationVersion{Speculative: true}}, 147 want: func(t *testing.T, active *Run, pending []*Run) { 148 assert.Equal(t, "run-active", active.ID) 149 if assert.Equal(t, 1, len(pending)) { 150 assert.Equal(t, "run-pending", pending[0].ID) 151 } 152 }, 153 }, 154 } 155 156 for _, tt := range tests { 157 t.Run(tt.name, func(t *testing.T) { 158 q := &WorkspaceQueue{ 159 PlanEnqueuer: &mockPlanEnqueuer{}, 160 Active: tt.active, 161 Pending: tt.pending, 162 } 163 164 q.Remove(tt.run) 165 166 tt.want(t, q.Active, q.Pending) 167 }) 168 } 169 }