mirror of https://github.com/grafana/grafana
Cloud migrations: create snapshot files (#89693)
* Cloud migrations: create snapshot and store it on disk * fix merge conflicts * implement StartSnapshot for gms client * pass snapshot directory as argument to snapshot builder * ensure snapshot folder is set * make swagger-gen * remove Test_ExecuteAsyncWorkflow * pass signed in user to buildSnapshot method / use github.com/grafana/grafana-cloud-migration-snapshot to create snapshot files * fix FakeServiceImpl.CreateSnapshot * remove new linepull/89999/head
parent
7b29242600
commit
d1952bb681
@ -0,0 +1,33 @@ |
||||
package slicesext |
||||
|
||||
import "math" |
||||
|
||||
// Partitions the input into slices where the length is <= chunkSize.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// Chunks(2, []int{1, 2, 3, 4})
|
||||
// => [][]int{{1, 2}, {3, 4}}
|
||||
func Chunks[T any](chunkSize int, xs []T) [][]T { |
||||
if chunkSize < 0 { |
||||
panic("chunk size must be greater than or equal to 0") |
||||
} |
||||
if chunkSize == 0 { |
||||
return [][]T{} |
||||
} |
||||
|
||||
out := make([][]T, 0, int(math.Ceil(float64(len(xs))/float64(chunkSize)))) |
||||
|
||||
for i := 0; i < len(xs); i += chunkSize { |
||||
var chunk []T |
||||
if i+chunkSize < len(xs) { |
||||
chunk = xs[i : i+chunkSize] |
||||
} else { |
||||
chunk = xs[i:] |
||||
} |
||||
|
||||
out = append(out, chunk) |
||||
} |
||||
|
||||
return out |
||||
} |
||||
@ -0,0 +1,80 @@ |
||||
package slicesext |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestChunks(t *testing.T) { |
||||
t.Parallel() |
||||
|
||||
t.Run("chunkSize must be greater than 0", func(t *testing.T) { |
||||
t.Parallel() |
||||
|
||||
assert.PanicsWithValue(t, "chunk size must be greater than or equal to 0", func() { |
||||
Chunks(-1, []string{}) |
||||
}) |
||||
}) |
||||
|
||||
t.Run("basic", func(t *testing.T) { |
||||
t.Parallel() |
||||
|
||||
cases := []struct { |
||||
description string |
||||
chunkSize int |
||||
input []int |
||||
expected [][]int |
||||
}{ |
||||
{ |
||||
description: "empty slice", |
||||
chunkSize: 2, |
||||
input: []int{}, |
||||
expected: [][]int{}, |
||||
}, |
||||
{ |
||||
description: "nil slice", |
||||
chunkSize: 2, |
||||
input: nil, |
||||
expected: [][]int{}, |
||||
}, |
||||
{ |
||||
description: "chunk size is 0", |
||||
chunkSize: 0, |
||||
input: []int{1, 2, 3}, |
||||
expected: [][]int{}, |
||||
}, |
||||
{ |
||||
description: "chunk size is greater than slice length", |
||||
chunkSize: 3, |
||||
input: []int{1}, |
||||
expected: [][]int{{1}}, |
||||
}, |
||||
{ |
||||
description: "chunk size is 1", |
||||
chunkSize: 1, |
||||
input: []int{1, 2, 3}, |
||||
expected: [][]int{{1}, {2}, {3}}, |
||||
}, |
||||
{ |
||||
description: "chunk size is 2 and slice length is 3", |
||||
chunkSize: 2, |
||||
input: []int{1, 2, 3}, |
||||
expected: [][]int{{1, 2}, {3}}, |
||||
}, |
||||
{ |
||||
description: "chunk size is 2 and slice length is 6", |
||||
chunkSize: 2, |
||||
input: []int{1, 2, 3, 4, 5, 6}, |
||||
expected: [][]int{{1, 2}, {3, 4}, {5, 6}}, |
||||
}, |
||||
} |
||||
|
||||
for _, tt := range cases { |
||||
t.Run(tt.description, func(t *testing.T) { |
||||
result := Chunks(tt.chunkSize, tt.input) |
||||
assert.Equal(t, tt.expected, result) |
||||
}) |
||||
} |
||||
}) |
||||
} |
||||
Loading…
Reference in new issue