checkpoint

mine
derailed 2019-11-14 18:46:46 -07:00
parent cf98f61ad6
commit 44f644fba7
11 changed files with 241 additions and 323 deletions

View File

@ -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)
}

View File

@ -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())
// }

View File

@ -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"))
}

View File

@ -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"))
// }

View File

@ -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()))
}

View File

@ -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()))
}

View File

@ -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

View File

@ -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"}
}

View File

@ -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)
// }
// }
// }

View File

@ -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)
}

View File

@ -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)
// }