Skip to content

Commit 6e72827

Browse files
committed
use fake fs for offline unit test
1 parent 3c868c9 commit 6e72827

File tree

4 files changed

+67
-32
lines changed

4 files changed

+67
-32
lines changed

cmd/skaffold/app/cmd/schema/print_test.go

+1-31
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,15 @@ package schema
1818

1919
import (
2020
"bytes"
21-
"io"
2221
"net/http"
23-
"os"
2422
"testing"
2523

2624
"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd/statik"
2725
"github.com/GoogleContainerTools/skaffold/testutil"
2826
)
2927

30-
type fakeFileSystem struct {
31-
Files map[string][]byte
32-
}
33-
34-
type fakeFile struct {
35-
http.File
36-
content io.Reader
37-
}
38-
39-
func (f *fakeFileSystem) Open(name string) (http.File, error) {
40-
content, found := f.Files[name]
41-
if !found {
42-
return nil, os.ErrNotExist
43-
}
44-
45-
return &fakeFile{
46-
content: bytes.NewBuffer(content),
47-
}, nil
48-
}
49-
50-
func (f *fakeFile) Read(p []byte) (n int, err error) {
51-
return f.content.Read(p)
52-
}
53-
54-
func (f *fakeFile) Close() error {
55-
return nil
56-
}
57-
5828
func TestPrint(t *testing.T) {
59-
fs := &fakeFileSystem{
29+
fs := &testutil.FakeFileSystem{
6030
Files: map[string][]byte{
6131
"/schemas/v1.json": []byte("{SCHEMA}"),
6232
},

pkg/skaffold/instrumentation/meter.go

-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ func init() {
104104

105105
func InitMeter(runCtx *runcontext.RunContext, config *latest.SkaffoldConfig) {
106106
meter.Command = runCtx.Opts.Command
107-
meter.PlatformType = yamltags.GetYamlTag(config.Build.BuildType)
108107
for _, artifact := range config.Pipeline.Build.Artifacts {
109108
if _, ok := meter.Builders[yamltags.GetYamlTag(artifact.ArtifactType)]; ok {
110109
meter.Builders[yamltags.GetYamlTag(artifact.ArtifactType)]++

pkg/skaffold/instrumentation/meter_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ import (
2020
"context"
2121
"encoding/json"
2222
"io/ioutil"
23+
"net/http"
2324
"os"
2425
"testing"
2526
"time"
2627

2728
"github.com/spf13/pflag"
2829

30+
"github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd/statik"
2931
"github.com/GoogleContainerTools/skaffold/proto"
3032
"github.com/GoogleContainerTools/skaffold/testutil"
3133
)
@@ -44,6 +46,17 @@ func TestOfflineExportMetrics(t *testing.T) {
4446
Duration: time.Minute,
4547
}
4648
validMeterBytes, _ := json.Marshal(validMeter)
49+
fs := &testutil.FakeFileSystem{
50+
Files: map[string][]byte{
51+
"/keys.json": []byte(`{
52+
"client_id": "test_id",
53+
"client_secret": "test_secret",
54+
"project_id": "test_project",
55+
"refresh_token": "test_token",
56+
"type": "authorized_user"
57+
}`),
58+
},
59+
}
4760

4861
tests := []struct {
4962
name string
@@ -87,6 +100,7 @@ func TestOfflineExportMetrics(t *testing.T) {
87100
for _, test := range tests {
88101
testutil.Run(t, test.name, func(t *testutil.T) {
89102
t.Override(&isOnline, false)
103+
t.Override(&statik.FS, func() (http.FileSystem, error) { return fs, nil })
90104
filename := "metrics"
91105
tmp := t.NewTempDir()
92106
var savedMetrics []skaffoldMeter

testutil/fake_fs.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2020 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package testutil
18+
19+
import (
20+
"bytes"
21+
"io"
22+
"net/http"
23+
"os"
24+
)
25+
26+
type FakeFileSystem struct {
27+
Files map[string][]byte
28+
}
29+
30+
type fakeFile struct {
31+
http.File
32+
content io.Reader
33+
}
34+
35+
func (f *FakeFileSystem) Open(name string) (http.File, error) {
36+
content, found := f.Files[name]
37+
if !found {
38+
return nil, os.ErrNotExist
39+
}
40+
41+
return &fakeFile{
42+
content: bytes.NewBuffer(content),
43+
}, nil
44+
}
45+
46+
func (f *fakeFile) Read(p []byte) (n int, err error) {
47+
return f.content.Read(p)
48+
}
49+
50+
func (f *fakeFile) Close() error {
51+
return nil
52+
}

0 commit comments

Comments
 (0)