mirror of https://github.com/k3s-io/k3s
94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
/*
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package etcd
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
"k8s.io/kubernetes/pkg/api/rest/resttest"
|
|
"k8s.io/kubernetes/pkg/registry/registrytest"
|
|
"k8s.io/kubernetes/pkg/tools"
|
|
"k8s.io/kubernetes/pkg/tools/etcdtest"
|
|
)
|
|
|
|
func newStorage(t *testing.T) (*REST, *tools.FakeEtcdClient) {
|
|
etcdStorage, fakeClient := registrytest.NewEtcdStorage(t)
|
|
return NewREST(etcdStorage), fakeClient
|
|
}
|
|
|
|
func validNewSecret(name string) *api.Secret {
|
|
return &api.Secret{
|
|
ObjectMeta: api.ObjectMeta{
|
|
Name: name,
|
|
Namespace: api.NamespaceDefault,
|
|
},
|
|
Data: map[string][]byte{
|
|
"test": []byte("data"),
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestCreate(t *testing.T) {
|
|
storage, fakeClient := newStorage(t)
|
|
test := resttest.New(t, storage, fakeClient.SetError)
|
|
secret := validNewSecret("foo")
|
|
secret.ObjectMeta = api.ObjectMeta{GenerateName: "foo-"}
|
|
test.TestCreate(
|
|
// valid
|
|
secret,
|
|
// invalid
|
|
&api.Secret{},
|
|
&api.Secret{
|
|
ObjectMeta: api.ObjectMeta{Name: "name"},
|
|
Data: map[string][]byte{"name with spaces": []byte("")},
|
|
},
|
|
&api.Secret{
|
|
ObjectMeta: api.ObjectMeta{Name: "name"},
|
|
Data: map[string][]byte{"~.dotfile": []byte("")},
|
|
},
|
|
)
|
|
}
|
|
|
|
func TestUpdate(t *testing.T) {
|
|
storage, fakeClient := newStorage(t)
|
|
test := resttest.New(t, storage, fakeClient.SetError)
|
|
key, err := storage.KeyFunc(test.TestContext(), "foo")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key = etcdtest.AddPrefix(key)
|
|
|
|
fakeClient.ExpectNotFoundGet(key)
|
|
fakeClient.ChangeIndex = 2
|
|
secret := validNewSecret("foo")
|
|
existing := validNewSecret("exists")
|
|
existing.Namespace = test.TestNamespace()
|
|
obj, err := storage.Create(test.TestContext(), existing)
|
|
if err != nil {
|
|
t.Fatalf("unable to create object: %v", err)
|
|
}
|
|
older := obj.(*api.Secret)
|
|
older.ResourceVersion = "1"
|
|
|
|
test.TestUpdate(
|
|
secret,
|
|
existing,
|
|
older,
|
|
)
|
|
}
|