From 44f644fba708e171d4d7519f42e992522d282a57 Mon Sep 17 00:00:00 2001 From: derailed Date: Thu, 14 Nov 2019 18:46:46 -0700 Subject: [PATCH] checkpoint --- internal/view/app_test.go | 24 +++--- internal/view/command_test.go | 19 ----- internal/view/details_int_test.go | 24 ++++++ internal/view/details_test.go | 25 ------ internal/view/dp_test.go | 26 +++--- internal/view/ds_test.go | 28 +++---- internal/view/job.go | 2 + internal/view/logs_test.go | 67 ++++++++++++---- internal/view/rbac_test.go | 106 ------------------------- internal/view/table_int_test.go | 127 ++++++++++++++++++++++++++++++ internal/view/table_test.go | 116 --------------------------- 11 files changed, 241 insertions(+), 323 deletions(-) delete mode 100644 internal/view/command_test.go create mode 100644 internal/view/details_int_test.go delete mode 100644 internal/view/details_test.go create mode 100644 internal/view/table_int_test.go delete mode 100644 internal/view/table_test.go diff --git a/internal/view/app_test.go b/internal/view/app_test.go index 0886260a..6e9fab90 100644 --- a/internal/view/app_test.go +++ b/internal/view/app_test.go @@ -1,17 +1,17 @@ package view_test -// import ( -// "testing" +import ( + "testing" -// "github.com/derailed/k9s/internal/config" -// "github.com/derailed/k9s/internal/view" -// "github.com/stretchr/testify/assert" -// ) + "github.com/derailed/k9s/internal/config" + "github.com/derailed/k9s/internal/view" + "github.com/stretchr/testify/assert" +) -// func TestAppNew(t *testing.T) { -// a := view.NewApp(config.NewConfig(ks{})) -// a.Init("blee", 10) +func TestAppNew(t *testing.T) { + a := view.NewApp(config.NewConfig(ks{})) + a.Init("blee", 10) -// assert.Equal(t, 11, len(a.GetActions())) -// assert.Equal(t, false, a.HasSkins) -// } + assert.Equal(t, 11, len(a.GetActions())) + assert.Equal(t, false, a.HasSkins) +} diff --git a/internal/view/command_test.go b/internal/view/command_test.go deleted file mode 100644 index 0e40f1a7..00000000 --- a/internal/view/command_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package view - -// import ( -// "testing" - -// "github.com/derailed/k9s/internal/config" -// "github.com/stretchr/testify/assert" -// ) - -// func TestCommandPush(t *testing.T) { -// c := newCommand(NewApp(config.NewConfig(ks{}))) -// c.pushCmd("fred") -// c.pushCmd("blee") -// p, top := c.previousCmd() - -// assert.Equal(t, "fred", p) -// assert.True(t, top) -// assert.True(t, c.lastCmd()) -// } diff --git a/internal/view/details_int_test.go b/internal/view/details_int_test.go new file mode 100644 index 00000000..8557644f --- /dev/null +++ b/internal/view/details_int_test.go @@ -0,0 +1,24 @@ +package view + +import ( + "testing" + + "github.com/derailed/k9s/internal/config" + "github.com/stretchr/testify/assert" +) + +func TestDetailsDecorateLines(t *testing.T) { + buff := ` + I love blee + blee is much [blue::]cooler [green::]than foo! + ` + exp := ` + I love ["0"]blee[""] + ["1"]blee[""] is much [blue::]cooler [green::]than foo! + ` + + app := NewApp(config.NewConfig(ks{})) + v := NewDetails(app, nil) + + assert.Equal(t, exp, v.decorateLines(buff, "blee")) +} diff --git a/internal/view/details_test.go b/internal/view/details_test.go deleted file mode 100644 index 22e9c909..00000000 --- a/internal/view/details_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package view_test - -// import ( -// "testing" - -// "github.com/derailed/k9s/internal/config" -// "github.com/derailed/k9s/internal/view" -// "github.com/stretchr/testify/assert" -// ) - -// func TestDetailsDecorateLines(t *testing.T) { -// buff := ` -// I love blee -// blee is much [blue::]cooler [green::]than foo! -// ` -// exp := ` -// I love ["0"]blee[""] -// ["1"]blee[""] is much [blue::]cooler [green::]than foo! -// ` - -// app := view.NewApp(config.NewConfig(ks{})) -// v := view.NewDetails{app: app} - -// assert.Equal(t, exp, v.decorateLines(buff, "blee")) -// } diff --git a/internal/view/dp_test.go b/internal/view/dp_test.go index da8a4ce2..574791a3 100644 --- a/internal/view/dp_test.go +++ b/internal/view/dp_test.go @@ -1,18 +1,18 @@ package view_test -// import ( -// "testing" +import ( + "testing" -// "github.com/derailed/k9s/internal/config" -// "github.com/derailed/k9s/internal/resource" -// "github.com/stretchr/testify/assert" -// ) + "github.com/derailed/k9s/internal/resource" + "github.com/derailed/k9s/internal/view" + "github.com/stretchr/testify/assert" +) -// func TestDeploy(t *testing.T) { -// l := resource.NewDeploymentList(nil, "fred") -// v := view.NewDeploy("blee", "", l) -// ctx := context.WithValue(ui.KeyApp, NewApp(config.NewConfig(ks{}))) -// v.Init(ctx) +func TestDeploy(t *testing.T) { + v := view.NewDeploy("Deploy", "", resource.NewDeploymentList(nil, "")) + v.Init(makeCtx()) -// assert.Equal(t, 10, len(v.Hints())) -// } + assert.Equal(t, "deploy", v.Name()) + assert.Equal(t, 24, len(v.Hints())) + +} diff --git a/internal/view/ds_test.go b/internal/view/ds_test.go index 734e2aa4..d2130ccc 100644 --- a/internal/view/ds_test.go +++ b/internal/view/ds_test.go @@ -1,21 +1,17 @@ package view_test -// import ( -// "context" -// "testing" +import ( + "testing" -// "github.com/derailed/k9s/internal/config" -// "github.com/derailed/k9s/internal/resource" -// "github.com/derailed/k9s/internal/ui" -// "github.com/derailed/k9s/internal/view" -// "github.com/stretchr/testify/assert" -// ) + "github.com/derailed/k9s/internal/resource" + "github.com/derailed/k9s/internal/view" + "github.com/stretchr/testify/assert" +) -// func TestDaemonSet(t *testing.T) { -// l := resource.NewDaemonSetList(nil, "fred") -// v := view.NewDaemonSet("blee", "", l) -// ctx := context.WithValue(ui.KeyApp, NewApp(config.NewConfig(ks{}))) -// v.Init(ctx) +func TestDaemonSet(t *testing.T) { + v := view.NewDaemonSet("blee", "", resource.NewDaemonSetList(nil, "")) + v.Init(makeCtx()) -// assert.Equal(t, 10, len(v.Hints())) -// } + assert.Equal(t, "ds", v.Name()) + assert.Equal(t, 23, len(v.Hints())) +} diff --git a/internal/view/job.go b/internal/view/job.go index 2d80f07e..50f989d3 100644 --- a/internal/view/job.go +++ b/internal/view/job.go @@ -8,10 +8,12 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +// Job represents a job viewer. type Job struct { *LogResource } +// NewJob returns a new viewer. func NewJob(title, gvr string, list resource.List) ResourceViewer { j := Job{NewLogResource(title, gvr, list)} j.extraActionsFn = j.extraActions diff --git a/internal/view/logs_test.go b/internal/view/logs_test.go index 693b30a1..b56158a5 100644 --- a/internal/view/logs_test.go +++ b/internal/view/logs_test.go @@ -1,21 +1,56 @@ package view -// func TestUpdateLogs(t *testing.T) { -// v := newLogView("test", NewApp(config.NewConfig(ks{})), nil) +import ( + "context" + "fmt" + "sync" + "testing" -// var wg sync.WaitGroup -// wg.Add(1) -// c := make(chan string, 10) -// go func() { -// defer wg.Done() -// updateLogs(context.Background(), c, v, 10) -// }() + "github.com/derailed/k9s/internal/config" + "github.com/stretchr/testify/assert" + v1 "k8s.io/api/core/v1" +) -// for i := 0; i < 500; i++ { -// c <- fmt.Sprintf("log %d", i) -// } -// close(c) -// wg.Wait() +func TestUpdateLogs(t *testing.T) { + v := NewLog("test", NewApp(config.NewConfig(ks{})), nil) -// assert.Equal(t, 500, v.logs.GetLineCount()) -// } + var wg sync.WaitGroup + wg.Add(1) + c := make(chan string, 10) + go func() { + defer wg.Done() + updateLogs(context.Background(), c, v, 10) + }() + + for i := 0; i < 500; i++ { + c <- fmt.Sprintf("log %d", i) + } + close(c) + wg.Wait() + + assert.Equal(t, 500, v.logs.GetLineCount()) +} + +// Helpers... + +type ks struct{} + +func (k ks) CurrentContextName() (string, error) { + return "test", nil +} + +func (k ks) CurrentClusterName() (string, error) { + return "test", nil +} + +func (k ks) CurrentNamespaceName() (string, error) { + return "test", nil +} + +func (k ks) ClusterNames() ([]string, error) { + return []string{"test"}, nil +} + +func (k ks) NamespaceNames(nn []v1.Namespace) []string { + return []string{"test"} +} diff --git a/internal/view/rbac_test.go b/internal/view/rbac_test.go index 2cb9f546..cb4beb9b 100644 --- a/internal/view/rbac_test.go +++ b/internal/view/rbac_test.go @@ -17,109 +17,3 @@ func TestRbacNew(t *testing.T) { assert.Equal(t, "Rbac", v.Name()) assert.Equal(t, 10, len(v.Hints())) } - -// func TestHasVerb(t *testing.T) { -// uu := []struct { -// vv []string -// v string -// e bool -// }{ -// {[]string{"*"}, "get", true}, -// {[]string{"get", "list", "watch"}, "watch", true}, -// {[]string{"get", "dope", "list"}, "watch", false}, -// {[]string{"get"}, "get", true}, -// {[]string{"post"}, "create", true}, -// {[]string{"put"}, "update", true}, -// {[]string{"list", "deletecollection"}, "deletecollection", true}, -// } - -// for _, u := range uu { -// assert.Equal(t, u.e, hasVerb(u.vv, u.v)) -// } -// } - -// func TestAsVerbs(t *testing.T) { -// ok, nok := toVerbIcon(true), toVerbIcon(false) - -// uu := []struct { -// vv []string -// e resource.Row -// }{ -// {[]string{"*"}, resource.Row{ok, ok, ok, ok, ok, ok, ok, ok, ""}}, -// {[]string{"get", "list", "patch"}, resource.Row{ok, ok, nok, nok, nok, ok, nok, nok, ""}}, -// {[]string{"get", "list", "deletecollection", "post"}, resource.Row{ok, ok, ok, nok, ok, nok, nok, nok, ""}}, -// {[]string{"get", "list", "blee"}, resource.Row{ok, ok, nok, nok, nok, nok, nok, nok, "blee"}}, -// } - -// for _, u := range uu { -// assert.Equal(t, u.e, asVerbs(u.vv...)) -// } -// } - -// func TestParseRules(t *testing.T) { -// ok, nok := toVerbIcon(true), toVerbIcon(false) -// _ = nok - -// uu := []struct { -// pp []rbacv1.PolicyRule -// e map[string]resource.Row -// }{ -// { -// []rbacv1.PolicyRule{ -// {APIGroups: []string{"*"}, Resources: []string{"*"}, Verbs: []string{"*"}}, -// }, -// map[string]resource.Row{ -// "*.*": {"*.*", "*", ok, ok, ok, ok, ok, ok, ok, ok, ""}, -// }, -// }, -// { -// []rbacv1.PolicyRule{ -// {APIGroups: []string{"*"}, Resources: []string{"*"}, Verbs: []string{"get"}}, -// }, -// map[string]resource.Row{ -// "*.*": {"*.*", "*", ok, nok, nok, nok, nok, nok, nok, nok, ""}, -// }, -// }, -// { -// []rbacv1.PolicyRule{ -// {APIGroups: []string{""}, Resources: []string{"*"}, Verbs: []string{"list"}}, -// }, -// map[string]resource.Row{ -// "*": {"*", "v1", nok, ok, nok, nok, nok, nok, nok, nok, ""}, -// }, -// }, -// { -// []rbacv1.PolicyRule{ -// {APIGroups: []string{""}, Resources: []string{"pods"}, Verbs: []string{"list"}, ResourceNames: []string{"fred"}}, -// }, -// map[string]resource.Row{ -// "pods": {"pods", "v1", nok, ok, nok, nok, nok, nok, nok, nok, ""}, -// "pods/fred": {"pods/fred", "v1", nok, ok, nok, nok, nok, nok, nok, nok, ""}, -// }, -// }, -// { -// []rbacv1.PolicyRule{ -// {APIGroups: []string{}, Resources: []string{}, Verbs: []string{"get"}, NonResourceURLs: []string{"/fred"}}, -// }, -// map[string]resource.Row{ -// "/fred": {"/fred", resource.NAValue, ok, nok, nok, nok, nok, nok, nok, nok, ""}, -// }, -// }, -// { -// []rbacv1.PolicyRule{ -// {APIGroups: []string{}, Resources: []string{}, Verbs: []string{"get"}, NonResourceURLs: []string{"fred"}}, -// }, -// map[string]resource.Row{ -// "/fred": {"/fred", resource.NAValue, ok, nok, nok, nok, nok, nok, nok, nok, ""}, -// }, -// }, -// } - -// var v view.Rbac -// for _, u := range uu { -// evts := v.parseRules(u.pp) -// for k, v := range u.e { -// assert.Equal(t, v, evts[k].Fields) -// } -// } -// } diff --git a/internal/view/table_int_test.go b/internal/view/table_int_test.go new file mode 100644 index 00000000..bde342ad --- /dev/null +++ b/internal/view/table_int_test.go @@ -0,0 +1,127 @@ +package view + +import ( + "context" + "io/ioutil" + "path/filepath" + "testing" + + "github.com/derailed/k9s/internal/config" + "github.com/derailed/k9s/internal/resource" + "github.com/derailed/k9s/internal/ui" + "github.com/stretchr/testify/assert" + "k8s.io/apimachinery/pkg/watch" +) + +func TestTableSave(t *testing.T) { + v := NewTable("test") + v.Init(makeContext()) + v.SetTitle("k9s-test") + + dir := filepath.Join(config.K9sDumpDir, v.app.Config.K9s.CurrentCluster) + c1, _ := ioutil.ReadDir(dir) + v.saveCmd(nil) + + c2, _ := ioutil.ReadDir(dir) + assert.Equal(t, len(c2), len(c1)+1) +} + +func TestTableNew(t *testing.T) { + v := NewTable("test") + v.Init(makeContext()) + + data := resource.TableData{ + Header: resource.Row{"NAMESPACE", "NAME", "FRED", "AGE"}, + Rows: resource.RowEvents{ + "ns1/a": &resource.RowEvent{ + Action: watch.Added, + Fields: resource.Row{"ns1", "a", "10", "3m"}, + Deltas: resource.Row{"", "", "", ""}, + }, + "ns1/b": &resource.RowEvent{ + Action: watch.Added, + Fields: resource.Row{"ns1", "b", "15", "1m"}, + Deltas: resource.Row{"", "", "20", ""}, + }, + }, + NumCols: map[string]bool{ + "FRED": true, + }, + Namespace: "", + } + v.Update(data) + assert.Equal(t, 3, v.GetRowCount()) +} + +func TestTableViewFilter(t *testing.T) { + v := NewTable("test") + v.Init(makeContext()) + + data := resource.TableData{ + Header: resource.Row{"NAMESPACE", "NAME", "FRED", "AGE"}, + Rows: resource.RowEvents{ + "ns1/blee": &resource.RowEvent{ + Action: watch.Added, + Fields: resource.Row{"ns1", "blee", "10", "3m"}, + Deltas: resource.Row{"", "", "", ""}, + }, + "ns1/fred": &resource.RowEvent{ + Action: watch.Added, + Fields: resource.Row{"ns1", "fred", "15", "1m"}, + Deltas: resource.Row{"", "", "20", ""}, + }, + }, + NumCols: map[string]bool{ + "FRED": true, + }, + Namespace: "", + } + v.Update(data) + v.SearchBuff().SetActive(true) + v.SearchBuff().Set("blee") + v.filterCmd(nil) + assert.Equal(t, 2, v.GetRowCount()) + v.resetCmd(nil) + assert.Equal(t, 3, v.GetRowCount()) +} + +func TestTableViewSort(t *testing.T) { + v := NewTable("test") + v.Init(makeContext()) + + data := resource.TableData{ + Header: resource.Row{"NAMESPACE", "NAME", "FRED", "AGE"}, + Rows: resource.RowEvents{ + "ns1/blee": &resource.RowEvent{ + Action: watch.Added, + Fields: resource.Row{"ns1", "blee", "10", "3m"}, + Deltas: resource.Row{"", "", "", ""}, + }, + "ns1/fred": &resource.RowEvent{ + Action: watch.Added, + Fields: resource.Row{"ns1", "fred", "15", "1m"}, + Deltas: resource.Row{"", "", "20", ""}, + }, + }, + NumCols: map[string]bool{ + "FRED": true, + }, + Namespace: "", + } + v.Update(data) + v.SortColCmd(1)(nil) + assert.Equal(t, 3, v.GetRowCount()) + assert.Equal(t, "blee ", v.GetCell(1, 1).Text) + + v.SortInvertCmd(nil) + assert.Equal(t, 3, v.GetRowCount()) + assert.Equal(t, "fred ", v.GetCell(1, 1).Text) +} + +// Helpers... + +func makeContext() context.Context { + a := NewApp(config.NewConfig(ks{})) + ctx := context.WithValue(context.Background(), ui.KeyApp, a) + return context.WithValue(ctx, ui.KeyStyles, a.Styles) +} diff --git a/internal/view/table_test.go b/internal/view/table_test.go deleted file mode 100644 index 24ed42f7..00000000 --- a/internal/view/table_test.go +++ /dev/null @@ -1,116 +0,0 @@ -package view_test - -// import ( -// "context" -// "io/ioutil" -// "path/filepath" -// "testing" - -// "github.com/derailed/k9s/internal/config" -// "github.com/derailed/k9s/internal/resource" -// "github.com/derailed/k9s/internal/ui" -// "github.com/derailed/k9s/internal/view" -// "github.com/stretchr/testify/assert" -// "k8s.io/apimachinery/pkg/watch" -// ) - -// func TestTableSave(t *testing.T) { -// v := view.NewTable("test") -// v.SetTitle("k9s-test") -// dir := filepath.Join(config.K9sDumpDir, v.app.Config.K9s.CurrentCluster) -// c1, _ := ioutil.ReadDir(dir) -// v.saveCmd(nil) -// c2, _ := ioutil.ReadDir(dir) -// assert.Equal(t, len(c2), len(c1)+1) -// } - -// func TestTableNew(t *testing.T) { -// v := view.NewTable("test") -// ctx := context.WithValue(ui.KeyApp, NewApp(config.NewConfig(ks{}))) -// v.Init(ctx) - -// data := resource.TableData{ -// Header: resource.Row{"NAMESPACE", "NAME", "FRED", "AGE"}, -// Rows: resource.RowEvents{ -// "ns1/a": &resource.RowEvent{ -// Action: watch.Added, -// Fields: resource.Row{"ns1", "a", "10", "3m"}, -// Deltas: resource.Row{"", "", "", ""}, -// }, -// "ns1/b": &resource.RowEvent{ -// Action: watch.Added, -// Fields: resource.Row{"ns1", "b", "15", "1m"}, -// Deltas: resource.Row{"", "", "20", ""}, -// }, -// }, -// NumCols: map[string]bool{ -// "FRED": true, -// }, -// Namespace: "", -// } -// v.Update(data) -// assert.Equal(t, 3, v.GetRowCount()) -// } - -// func TestTableViewFilter(t *testing.T) { -// v := newTableView(NewApp(config.NewConfig(ks{})), "test") - -// data := resource.TableData{ -// Header: resource.Row{"NAMESPACE", "NAME", "FRED", "AGE"}, -// Rows: resource.RowEvents{ -// "ns1/blee": &resource.RowEvent{ -// Action: watch.Added, -// Fields: resource.Row{"ns1", "blee", "10", "3m"}, -// Deltas: resource.Row{"", "", "", ""}, -// }, -// "ns1/fred": &resource.RowEvent{ -// Action: watch.Added, -// Fields: resource.Row{"ns1", "fred", "15", "1m"}, -// Deltas: resource.Row{"", "", "20", ""}, -// }, -// }, -// NumCols: map[string]bool{ -// "FRED": true, -// }, -// Namespace: "", -// } -// v.Update(data) -// v.SearchBuff().SetActive(true) -// v.SearchBuff().Set("blee") -// v.filterCmd(nil) -// assert.Equal(t, 2, v.GetRowCount()) -// v.resetCmd(nil) -// assert.Equal(t, 3, v.GetRowCount()) -// } - -// func TestTableViewSort(t *testing.T) { -// v := newTableView(NewApp(config.NewConfig(ks{})), "test") - -// data := resource.TableData{ -// Header: resource.Row{"NAMESPACE", "NAME", "FRED", "AGE"}, -// Rows: resource.RowEvents{ -// "ns1/blee": &resource.RowEvent{ -// Action: watch.Added, -// Fields: resource.Row{"ns1", "blee", "10", "3m"}, -// Deltas: resource.Row{"", "", "", ""}, -// }, -// "ns1/fred": &resource.RowEvent{ -// Action: watch.Added, -// Fields: resource.Row{"ns1", "fred", "15", "1m"}, -// Deltas: resource.Row{"", "", "20", ""}, -// }, -// }, -// NumCols: map[string]bool{ -// "FRED": true, -// }, -// Namespace: "", -// } -// v.Update(data) -// v.SortColCmd(1)(nil) -// assert.Equal(t, 3, v.GetRowCount()) -// assert.Equal(t, "blee ", v.GetCell(1, 1).Text) - -// v.SortInvertCmd(nil) -// assert.Equal(t, 3, v.GetRowCount()) -// assert.Equal(t, "fred ", v.GetCell(1, 1).Text) -// }