github.com/replicatedcom/ship@v0.50.0/pkg/specs/replicatedapp/local_test.go (about) 1 package replicatedapp 2 3 import ( 4 "testing" 5 6 "github.com/replicatedhq/ship/pkg/state" 7 "github.com/replicatedhq/ship/pkg/testing/logger" 8 "github.com/spf13/afero" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestLoadLocalGitHubContents(t *testing.T) { 13 tests := []struct { 14 name string 15 githubContent []string 16 fs map[string]string 17 expectContents []state.GithubContent 18 }{ 19 { 20 name: "none to set", 21 githubContent: nil, 22 expectContents: []state.GithubContent{}, 23 }, 24 { 25 name: "set one file", 26 fs: map[string]string{ 27 "/foo/bar.txt": "some-contents", 28 }, 29 githubContent: []string{"replicatedhq/test-stuff:/bar.txt:master:/foo"}, 30 expectContents: []state.GithubContent{ 31 { 32 Repo: "replicatedhq/test-stuff", 33 Path: "/bar.txt", 34 Ref: "master", 35 Files: []state.GithubFile{ 36 { 37 Path: "/bar.txt", 38 Name: "bar.txt", 39 Size: 13, 40 Sha: "6e32ea34db1b3755d7dec972eb72c705338f0dd8e0be881d966963438fb2e800", 41 Data: "c29tZS1jb250ZW50cw==", 42 }, 43 }, 44 }, 45 }, 46 }, 47 { 48 name: "set many files from two repos", 49 fs: map[string]string{ 50 "/foo/bar.txt": "some-contents", 51 "/foo/baz.txt": "some-contents", 52 "/foo/bar/baz.txt": "some-contents", 53 "/spam/eggs.txt": "some-other-contents", 54 }, 55 githubContent: []string{ 56 "replicatedhq/test-stuff:/:master:/foo", 57 "replicatedhq/other-tests:/eggs.txt:release:/spam", 58 }, 59 expectContents: []state.GithubContent{ 60 { 61 Repo: "replicatedhq/test-stuff", 62 Path: "/", 63 Ref: "master", 64 Files: []state.GithubFile{ 65 { 66 Path: "/bar/baz.txt", 67 Name: "baz.txt", 68 Size: 13, 69 Sha: "6e32ea34db1b3755d7dec972eb72c705338f0dd8e0be881d966963438fb2e800", 70 Data: "c29tZS1jb250ZW50cw==", 71 }, 72 { 73 Path: "/bar.txt", 74 Name: "bar.txt", 75 Size: 13, 76 Sha: "6e32ea34db1b3755d7dec972eb72c705338f0dd8e0be881d966963438fb2e800", 77 Data: "c29tZS1jb250ZW50cw==", 78 }, 79 { 80 Path: "/baz.txt", 81 Name: "baz.txt", 82 Size: 13, 83 Sha: "6e32ea34db1b3755d7dec972eb72c705338f0dd8e0be881d966963438fb2e800", 84 Data: "c29tZS1jb250ZW50cw==", 85 }, 86 }, 87 }, 88 { 89 Repo: "replicatedhq/other-tests", 90 Path: "/eggs.txt", 91 Ref: "release", 92 Files: []state.GithubFile{ 93 { 94 Path: "/eggs.txt", 95 Name: "eggs.txt", 96 Size: 19, 97 Sha: "a2c0a8c54d71e14e9533749c32716c12f92f61294dfdce4f3b4c07303c0119b0", 98 Data: "c29tZS1vdGhlci1jb250ZW50cw==", 99 }, 100 }, 101 }, 102 }, 103 }, 104 } 105 for _, test := range tests { 106 t.Run(test.name, func(t *testing.T) { 107 req := require.New(t) 108 mockFs := afero.Afero{Fs: afero.NewMemMapFs()} 109 110 for key, value := range test.fs { 111 err := mockFs.WriteFile(key, []byte(value), 0777) 112 req.NoError(err) 113 } 114 115 resolver := &resolver{ 116 Logger: &logger.TestLogger{T: t}, 117 FS: mockFs, 118 SetGitHubContents: test.githubContent, 119 } 120 121 result, err := resolver.loadLocalGitHubContents() 122 123 req.NoError(err) 124 req.Equal(test.expectContents, result) 125 }) 126 } 127 }