mirror of https://github.com/k3s-io/k3s
210 lines
6.5 KiB
Go
210 lines
6.5 KiB
Go
/*
|
|
Copyright 2014 Google Inc. 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 controller
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
|
fake_cloud "github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider/fake"
|
|
)
|
|
|
|
func newMinion(name string) *api.Minion {
|
|
return &api.Minion{ObjectMeta: api.ObjectMeta{Name: name}}
|
|
}
|
|
|
|
type FakeMinionHandler struct {
|
|
client.Fake
|
|
client.FakeMinions
|
|
|
|
// Input: Hooks determine if request is valid or not
|
|
CreateHook func(*FakeMinionHandler, *api.Minion) bool
|
|
Existing []*api.Minion
|
|
|
|
// Output
|
|
CreatedMinions []*api.Minion
|
|
DeletedMinions []*api.Minion
|
|
RequestCount int
|
|
}
|
|
|
|
func (c *FakeMinionHandler) Minions() client.MinionInterface {
|
|
return c
|
|
}
|
|
|
|
func (m *FakeMinionHandler) Create(minion *api.Minion) (*api.Minion, error) {
|
|
defer func() { m.RequestCount++ }()
|
|
if m.CreateHook == nil || m.CreateHook(m, minion) {
|
|
m.CreatedMinions = append(m.CreatedMinions, minion)
|
|
return minion, nil
|
|
} else {
|
|
return nil, fmt.Errorf("Create error.")
|
|
}
|
|
}
|
|
|
|
func (m *FakeMinionHandler) List() (*api.MinionList, error) {
|
|
defer func() { m.RequestCount++ }()
|
|
minions := []api.Minion{}
|
|
for i := 0; i < len(m.Existing); i++ {
|
|
if !contains(m.Existing[i], m.DeletedMinions) {
|
|
minions = append(minions, *m.Existing[i])
|
|
}
|
|
}
|
|
for i := 0; i < len(m.CreatedMinions); i++ {
|
|
if !contains(m.Existing[i], m.DeletedMinions) {
|
|
minions = append(minions, *m.CreatedMinions[i])
|
|
}
|
|
}
|
|
return &api.MinionList{Items: minions}, nil
|
|
}
|
|
|
|
func (m *FakeMinionHandler) Delete(id string) error {
|
|
m.DeletedMinions = append(m.DeletedMinions, newMinion(id))
|
|
m.RequestCount++
|
|
return nil
|
|
}
|
|
|
|
func TestSyncStaticCreateMinion(t *testing.T) {
|
|
fakeMinionHandler := &FakeMinionHandler{
|
|
CreateHook: func(fake *FakeMinionHandler, minion *api.Minion) bool {
|
|
return true
|
|
},
|
|
}
|
|
minionController := NewMinionController(nil, ".*", []string{"minion0"}, &api.NodeResources{}, fakeMinionHandler)
|
|
if err := minionController.SyncStatic(time.Millisecond); err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
|
|
if fakeMinionHandler.RequestCount != 1 {
|
|
t.Errorf("Expected 1 call, but got %v.", fakeMinionHandler.RequestCount)
|
|
}
|
|
if len(fakeMinionHandler.CreatedMinions) != 1 {
|
|
t.Errorf("expect only 1 minion created, got %v", len(fakeMinionHandler.CreatedMinions))
|
|
}
|
|
if fakeMinionHandler.CreatedMinions[0].Name != "minion0" {
|
|
t.Errorf("unexpect minion %v created", fakeMinionHandler.CreatedMinions[0].Name)
|
|
}
|
|
}
|
|
|
|
func TestSyncStaticCreateMinionWithError(t *testing.T) {
|
|
fakeMinionHandler := &FakeMinionHandler{
|
|
CreateHook: func(fake *FakeMinionHandler, minion *api.Minion) bool {
|
|
if fake.RequestCount == 0 {
|
|
return false
|
|
}
|
|
return true
|
|
},
|
|
}
|
|
minionController := NewMinionController(nil, ".*", []string{"minion0"}, &api.NodeResources{}, fakeMinionHandler)
|
|
if err := minionController.SyncStatic(time.Millisecond); err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
|
|
if fakeMinionHandler.RequestCount != 2 {
|
|
t.Errorf("Expected 2 call, but got %v.", fakeMinionHandler.RequestCount)
|
|
}
|
|
if len(fakeMinionHandler.CreatedMinions) != 1 {
|
|
t.Errorf("expect only 1 minion created, got %v", len(fakeMinionHandler.CreatedMinions))
|
|
}
|
|
if fakeMinionHandler.CreatedMinions[0].Name != "minion0" {
|
|
t.Errorf("unexpect minion %v created", fakeMinionHandler.CreatedMinions[0].Name)
|
|
}
|
|
}
|
|
|
|
func TestSyncCloudCreateMinion(t *testing.T) {
|
|
fakeMinionHandler := &FakeMinionHandler{
|
|
Existing: []*api.Minion{newMinion("minion0")},
|
|
}
|
|
instances := []string{"minion0", "minion1"}
|
|
fakeCloud := fake_cloud.FakeCloud{
|
|
Machines: instances,
|
|
}
|
|
minionController := NewMinionController(&fakeCloud, ".*", nil, nil, fakeMinionHandler)
|
|
if err := minionController.SyncCloud(); err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
|
|
if fakeMinionHandler.RequestCount != 2 {
|
|
t.Errorf("Expected 2 call, but got %v.", fakeMinionHandler.RequestCount)
|
|
}
|
|
if len(fakeMinionHandler.CreatedMinions) != 1 {
|
|
t.Errorf("expect only 1 minion created, got %v", len(fakeMinionHandler.CreatedMinions))
|
|
}
|
|
if fakeMinionHandler.CreatedMinions[0].Name != "minion1" {
|
|
t.Errorf("unexpect minion %v created", fakeMinionHandler.CreatedMinions[0].Name)
|
|
}
|
|
}
|
|
|
|
func TestSyncCloudDeleteMinion(t *testing.T) {
|
|
fakeMinionHandler := &FakeMinionHandler{
|
|
Existing: []*api.Minion{newMinion("minion0"), newMinion("minion1")},
|
|
}
|
|
instances := []string{"minion0"}
|
|
fakeCloud := fake_cloud.FakeCloud{
|
|
Machines: instances,
|
|
}
|
|
minionController := NewMinionController(&fakeCloud, ".*", nil, nil, fakeMinionHandler)
|
|
if err := minionController.SyncCloud(); err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
|
|
if fakeMinionHandler.RequestCount != 2 {
|
|
t.Errorf("Expected 2 call, but got %v.", fakeMinionHandler.RequestCount)
|
|
}
|
|
if len(fakeMinionHandler.DeletedMinions) != 1 {
|
|
t.Errorf("expect only 1 minion deleted, got %v", len(fakeMinionHandler.DeletedMinions))
|
|
}
|
|
if fakeMinionHandler.DeletedMinions[0].Name != "minion1" {
|
|
t.Errorf("unexpect minion %v created", fakeMinionHandler.DeletedMinions[0].Name)
|
|
}
|
|
}
|
|
|
|
func TestSyncCloudRegexp(t *testing.T) {
|
|
fakeMinionHandler := &FakeMinionHandler{
|
|
Existing: []*api.Minion{newMinion("minion0")},
|
|
}
|
|
instances := []string{"minion0", "minion1", "node0"}
|
|
fakeCloud := fake_cloud.FakeCloud{
|
|
Machines: instances,
|
|
}
|
|
minionController := NewMinionController(&fakeCloud, "minion[0-9]+", nil, nil, fakeMinionHandler)
|
|
if err := minionController.SyncCloud(); err != nil {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
|
|
if fakeMinionHandler.RequestCount != 2 {
|
|
t.Errorf("Expected 2 call, but got %v.", fakeMinionHandler.RequestCount)
|
|
}
|
|
if len(fakeMinionHandler.CreatedMinions) != 1 {
|
|
t.Errorf("expect only 1 minion created, got %v", len(fakeMinionHandler.CreatedMinions))
|
|
}
|
|
if fakeMinionHandler.CreatedMinions[0].Name != "minion1" {
|
|
t.Errorf("unexpect minion %v created", fakeMinionHandler.CreatedMinions[0].Name)
|
|
}
|
|
}
|
|
|
|
func contains(minion *api.Minion, minions []*api.Minion) bool {
|
|
for i := 0; i < len(minions); i++ {
|
|
if minion.Name == minions[i].Name {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|