github.com/replicatedhq/ship@v0.55.0/pkg/specs/stategetter/client_test.go (about)

     1  package stategetter
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/replicatedhq/ship/pkg/state"
    10  	"github.com/replicatedhq/ship/pkg/testing/logger"
    11  	"github.com/spf13/afero"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestStateGetter_GetFiles(t *testing.T) {
    16  	type fileType struct {
    17  		Path     string
    18  		Contents string
    19  		Perms    os.FileMode
    20  	}
    21  
    22  	tests := []struct {
    23  		name            string
    24  		upstream        string
    25  		destinationPath string
    26  		want            string
    27  		wantErr         bool
    28  		contents        state.UpstreamContents
    29  		inputFS         []fileType
    30  		outputFS        []fileType
    31  	}{
    32  		{
    33  			name:            "no contents",
    34  			upstream:        "blah",
    35  			destinationPath: "/hello-world",
    36  			want:            "/hello-world/state",
    37  			contents:        state.UpstreamContents{},
    38  			inputFS:         []fileType{},
    39  			outputFS:        []fileType{},
    40  		},
    41  		{
    42  			name:            "nested contents",
    43  			upstream:        "blah",
    44  			destinationPath: "/hello-world",
    45  			want:            "/hello-world/state",
    46  			contents: state.UpstreamContents{
    47  				UpstreamFiles: []state.UpstreamFile{
    48  					{FilePath: "README.md", FileContents: "SGVsbG8gV29ybGQh"},
    49  					{FilePath: "/nest/me/GOODBYE.md", FileContents: "R29vZGJ5ZQ=="},
    50  				},
    51  			},
    52  			inputFS: []fileType{},
    53  			outputFS: []fileType{
    54  				{
    55  					Path:     "/hello-world/state/README.md",
    56  					Contents: "Hello World!",
    57  					Perms:    0755,
    58  				},
    59  				{
    60  					Path:     "/hello-world/state/nest/me/GOODBYE.md",
    61  					Contents: "Goodbye",
    62  					Perms:    0755,
    63  				},
    64  			},
    65  		},
    66  		{
    67  			name:            "overwrite contents",
    68  			upstream:        "blah",
    69  			destinationPath: "/hello-world",
    70  			want:            "/hello-world/state",
    71  			contents: state.UpstreamContents{
    72  				UpstreamFiles: []state.UpstreamFile{
    73  					{FilePath: "README.md", FileContents: "SGVsbG8gV29ybGQh"},
    74  					{FilePath: "/nest/me/GOODBYE.md", FileContents: "R29vZGJ5ZQ=="},
    75  				},
    76  			},
    77  			inputFS: []fileType{
    78  				{
    79  					Path:     "/hello-world/state/README.md",
    80  					Contents: "Should Not Exist",
    81  					Perms:    0755,
    82  				},
    83  			},
    84  			outputFS: []fileType{
    85  				{
    86  					Path:     "/hello-world/state/README.md",
    87  					Contents: "Hello World!",
    88  					Perms:    0755,
    89  				},
    90  				{
    91  					Path:     "/hello-world/state/nest/me/GOODBYE.md",
    92  					Contents: "Goodbye",
    93  					Perms:    0755,
    94  				},
    95  			},
    96  		},
    97  		{
    98  			name:            "not base64 contents",
    99  			upstream:        "blah",
   100  			destinationPath: "/hello-world",
   101  			want:            "",
   102  			wantErr:         true,
   103  			contents: state.UpstreamContents{
   104  				UpstreamFiles: []state.UpstreamFile{
   105  					{FilePath: "README.md", FileContents: "SGVsbG8gV29ybGQh"},
   106  					{FilePath: "/nest/me/GOODBYE.md", FileContents: "this-is-not-valid-base64"},
   107  				},
   108  			},
   109  			inputFS: []fileType{
   110  				{
   111  					Path:     "/hello-world/state/README.md",
   112  					Contents: "Should Not Exist",
   113  					Perms:    0755,
   114  				},
   115  			},
   116  			outputFS: []fileType{
   117  				{
   118  					Path:     "/hello-world/state/README.md",
   119  					Contents: "Hello World!",
   120  					Perms:    0755,
   121  				},
   122  			},
   123  		},
   124  	}
   125  	for _, tt := range tests {
   126  		t.Run(tt.name, func(t *testing.T) {
   127  			req := require.New(t)
   128  
   129  			tlog := logger.TestLogger{T: t}
   130  			fs := afero.Afero{Fs: afero.NewMemMapFs()}
   131  			g := &StateGetter{
   132  				Logger:   &tlog,
   133  				Contents: &tt.contents,
   134  				Fs:       fs,
   135  			}
   136  
   137  			// populate input filesystem
   138  			for _, file := range tt.inputFS {
   139  				err := fs.MkdirAll(filepath.Dir(file.Path), file.Perms)
   140  				req.NoError(err)
   141  
   142  				err = fs.WriteFile(file.Path, []byte(file.Contents), file.Perms)
   143  				req.NoError(err)
   144  			}
   145  
   146  			got, err := g.GetFiles(context.Background(), tt.upstream, tt.destinationPath)
   147  			if tt.wantErr {
   148  				req.Error(err)
   149  			} else {
   150  				req.NoError(err)
   151  			}
   152  			req.Equal(tt.want, got)
   153  
   154  			// compare resulting filesystems
   155  			for _, file := range tt.outputFS {
   156  				info, err := fs.Stat(file.Path)
   157  				req.NoError(err)
   158  				req.Equal(file.Perms, info.Mode())
   159  
   160  				contents, err := fs.ReadFile(file.Path)
   161  				req.NoError(err)
   162  				req.Equal(file.Contents, string(contents))
   163  			}
   164  		})
   165  	}
   166  }