generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
- Loading branch information
1 parent
454fc9a
commit 8640c1b
Showing
2 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package openapiclient | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"reflect" | ||
"testing" | ||
|
||
"k8s.io/client-go/openapi" | ||
) | ||
|
||
func TestNewLocalCRDFiles(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
fs fs.FS | ||
dirPath string | ||
want openapi.Client | ||
}{{ | ||
name: "fs nil and dir empty", | ||
want: &localCRDsClient{}, | ||
}, { | ||
name: "only dir", | ||
dirPath: "test", | ||
want: &localCRDsClient{ | ||
dir: "test", | ||
}, | ||
}, { | ||
name: "only fs", | ||
fs: os.DirFS("."), | ||
want: &localCRDsClient{ | ||
fs: os.DirFS("."), | ||
}, | ||
}, { | ||
name: "both fs and dir", | ||
fs: os.DirFS("."), | ||
dirPath: "test", | ||
want: &localCRDsClient{ | ||
fs: os.DirFS("."), | ||
dir: "test", | ||
}, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := NewLocalCRDFiles(tt.fs, tt.dirPath); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("NewLocalCRDFiles() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_localCRDsClient_Paths(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
fs fs.FS | ||
dir string | ||
want map[string]struct{} | ||
wantErr bool | ||
}{{ | ||
name: "fs nil and dir empty", | ||
}, { | ||
name: "only dir", | ||
dir: "../../testcases/crds", | ||
want: map[string]struct{}{ | ||
"apis/batch.x-k8s.io/v1alpha1": {}, | ||
"apis/stable.example.com/v1": {}, | ||
}, | ||
}, { | ||
name: "only fs", | ||
fs: os.DirFS("../../testcases/crds"), | ||
}, { | ||
name: "both fs and dir", | ||
fs: os.DirFS("../../testcases"), | ||
dir: "crds", | ||
want: map[string]struct{}{ | ||
"apis/batch.x-k8s.io/v1alpha1": {}, | ||
"apis/stable.example.com/v1": {}, | ||
}, | ||
}, { | ||
name: "invalid dir", | ||
dir: "invalid", | ||
wantErr: true, | ||
}, { | ||
name: "invalid fs", | ||
fs: os.DirFS("../../invalid"), | ||
dir: ".", | ||
wantErr: true, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
k := NewLocalCRDFiles(tt.fs, tt.dir) | ||
paths, err := k.Paths() | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("localCRDsClient.Paths() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if tt.want == nil && paths != nil || tt.want != nil && paths == nil { | ||
t.Errorf("localCRDsClient.Paths() = %v, want %v", paths, tt.want) | ||
return | ||
} | ||
if tt.want != nil && paths != nil { | ||
got := map[string]struct{}{} | ||
for key := range paths { | ||
got[key] = struct{}{} | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("localCRDsClient.Paths() = %v, want %v", got, tt.want) | ||
} | ||
} | ||
}) | ||
} | ||
} |