Using assertions

Using assertions for unit tests:

1. cmd/kube-controller-manager/app/controller_manager_test.go
2. pkg/controller/bootstrap/jws_test.go
3. pkg/controller/cloud/node_controller_test.go
4. pkg/controller/controller_utils_test.go
This commit is contained in:
Chen Li
2017-09-20 00:24:07 -05:00
parent 1797255cd2
commit fb9b29dbb1
7 changed files with 166 additions and 221 deletions

View File

@@ -16,7 +16,11 @@ limitations under the License.
package bootstrap
import "testing"
import (
"testing"
"github.com/stretchr/testify/assert"
)
const (
content = "Hello from the other side. I must have called a thousand times."
@@ -26,42 +30,40 @@ const (
func TestComputeDetachedSig(t *testing.T) {
sig, err := computeDetachedSig(content, id, secret)
if err != nil {
t.Errorf("Error when computing signature: %v", err)
}
if sig != "eyJhbGciOiJIUzI1NiIsImtpZCI6Impvc2h1YSJ9..VShe2taLd-YTrmWuRkcL_8QTNDHYxQIEBsAYYiIj1_8" {
t.Errorf("Wrong signature. Got: %v", sig)
}
assert.NoError(t, err, "Error when computing signature: %v", err)
assert.Equal(
t,
"eyJhbGciOiJIUzI1NiIsImtpZCI6Impvc2h1YSJ9..VShe2taLd-YTrmWuRkcL_8QTNDHYxQIEBsAYYiIj1_8",
sig,
"Wrong signature. Got: %v", sig)
// Try with null content
sig, err = computeDetachedSig("", id, secret)
if err != nil {
t.Errorf("Error when computing signature: %v", err)
}
if sig != "eyJhbGciOiJIUzI1NiIsImtpZCI6Impvc2h1YSJ9..7Ui1ALizW4jXphVUB7xUqC9vLYLL9RZeOFfVLoB7Tgk" {
t.Errorf("Wrong signature. Got: %v", sig)
}
assert.NoError(t, err, "Error when computing signature: %v", err)
assert.Equal(
t,
"eyJhbGciOiJIUzI1NiIsImtpZCI6Impvc2h1YSJ9..7Ui1ALizW4jXphVUB7xUqC9vLYLL9RZeOFfVLoB7Tgk",
sig,
"Wrong signature. Got: %v", sig)
// Try with no secret
sig, err = computeDetachedSig(content, id, "")
if err != nil {
t.Errorf("Error when computing signature: %v", err)
}
if sig != "eyJhbGciOiJIUzI1NiIsImtpZCI6Impvc2h1YSJ9..UfkqvDGiIFxrMnFseDj9LYJOLNrvjW8aHhF71mvvAs8" {
t.Errorf("Wrong signature. Got: %v", sig)
}
assert.NoError(t, err, "Error when computing signature: %v", err)
assert.Equal(
t,
"eyJhbGciOiJIUzI1NiIsImtpZCI6Impvc2h1YSJ9..UfkqvDGiIFxrMnFseDj9LYJOLNrvjW8aHhF71mvvAs8",
sig,
"Wrong signature. Got: %v", sig)
}
func TestDetachedTokenIsValid(t *testing.T) {
// Valid detached JWS token and valid inputs should succeed
sig := "eyJhbGciOiJIUzI1NiIsImtpZCI6Impvc2h1YSJ9..VShe2taLd-YTrmWuRkcL_8QTNDHYxQIEBsAYYiIj1_8"
if !DetachedTokenIsValid(sig, content, id, secret) {
t.Errorf("Content %q and token \"%s:%s\" should equal signature: %q", content, id, secret, sig)
}
assert.True(t, DetachedTokenIsValid(sig, content, id, secret),
"Content %q and token \"%s:%s\" should equal signature: %q", content, id, secret, sig)
// Invalid detached JWS token and valid inputs should fail
sig2 := sig + "foo"
if DetachedTokenIsValid(sig2, content, id, secret) {
t.Errorf("Content %q and token \"%s:%s\" should not equal signature: %q", content, id, secret, sig)
}
assert.False(t, DetachedTokenIsValid(sig2, content, id, secret),
"Content %q and token \"%s:%s\" should not equal signature: %q", content, id, secret, sig)
}