Skip to content

Commit

Permalink
update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwood committed Oct 7, 2024
1 parent 4372b67 commit 983b9d4
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 30 deletions.
181 changes: 181 additions & 0 deletions kaleido/platform/authentictor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// Copyright © Kaleido, Inc. 2024

// 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 platform

import (
"fmt"
"net/http"
"testing"
"time"

"github.com/aidarkhanov/nanoid"
"github.com/gorilla/mux"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/stretchr/testify/assert"

_ "embed"
)

var authenticatorStep1 = `
resource "kaleido_platform_authenticator" "authenticator1" {
environment = "env1"
network = "net1"
type = "permitted"
name = "auth1"
zone = "zone1"
conn = "conn1"
permitted_json = jsonencode({"peers":[{"endpoints":[{"host":"10.244.3.64","nat":"None","port":30303,"protocol":"TCP"},{"host":"86.13.78.205","nat":"Source","port":30303,"protocol":"TCP"}],"identity":"496f2bfe5cac576cb33f98778eb5617e3d3fe2e9ffeda8e7d0bde22f5e15d2dd4750f59a268ece9197aa10f4e709012564b514782ea86529c11d02a3c604ee7b"}]})
}
`

var authenticatorStep2 = `
resource "kaleido_platform_authenticator" "authenticator1" {
environment = "env1"
network = "net1"
type = "permitted"
name = "auth1"
zone = "zone1"
conn = "conn1"
permitted_json = jsonencode({"peers":[{"endpoints":[{"host":"10.244.3.64","nat":"None","port":30303,"protocol":"TCP"},{"host":"86.13.78.205","nat":"Source","port":30303,"protocol":"TCP"}],"identity":"496f2bfe5cac576cb33f98778eb5617e3d3fe2e9ffeda8e7d0bde22f5e15d2dd4750f59a268ece9197aa10f4e709012564b514782ea86529c11d02a3c604ee7b"}]})
}
`

func TestAuthenticator(t *testing.T) {

mp, providerConfig := testSetup(t)
defer func() {
mp.checkClearCalls([]string{
"POST /api/v1/environments/{env}/networks/{net}/authenticators",
"GET /api/v1/environments/{env}/networks/{net}/authenticators/{authenticator}",
"GET /api/v1/environments/{env}/networks/{net}/authenticators/{authenticator}",
"GET /api/v1/environments/{env}/networks/{net}/authenticators/{authenticator}",
"GET /api/v1/environments/{env}/networks/{net}/authenticators/{authenticator}",
"GET /api/v1/environments/{env}/networks/{net}/authenticators/{authenticator}",
"DELETE /api/v1/environments/{env}/networks/{net}/authenticators/{authenticator}",
"GET /api/v1/environments/{env}/networks/{net}/authenticators/{authenticator}",
})
mp.server.Close()
}()

authenticator1Resource := "kaleido_platform_authenticator.authenticator1"
resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: providerConfig + authenticatorStep1,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(authenticator1Resource, "id"),
),
},
{
Config: providerConfig + authenticatorStep2,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(authenticator1Resource, "id"),
func(s *terraform.State) error {
// Compare the final result on the mock-server side
id := s.RootModule().Resources[authenticator1Resource].Primary.Attributes["id"]
auth := mp.authenticators[fmt.Sprintf("env1/net1/%s", id)]
testJSONEqual(t, auth, fmt.Sprintf(`
{
"id": "%[1]s",
"created": "%[2]s",
"updated": "%[3]s",
"type": "permitted",
"name": "auth1",
"networkId": "net1",
"permitted": {
"peers": [
{
"endpoints": [
{
"host": "10.244.3.64",
"nat": "None",
"port": 30303,
"protocol": "TCP"
},
{
"host": "86.13.78.205",
"nat": "Source",
"port": 30303,
"protocol": "TCP"
}
],
"identity": "496f2bfe5cac576cb33f98778eb5617e3d3fe2e9ffeda8e7d0bde22f5e15d2dd4750f59a268ece9197aa10f4e709012564b514782ea86529c11d02a3c604ee7b"
}
]
},
"zone": "zone1",
"connection": "conn1",
"status": "ready"
}
`,
// generated fields that vary per test run
id,
auth.Created.UTC().Format(time.RFC3339Nano),
auth.Updated.UTC().Format(time.RFC3339Nano),
))
return nil
},
),
},
},
})
}

func (mp *mockPlatform) getAuthenticator(res http.ResponseWriter, req *http.Request) {
auth := mp.authenticators[mux.Vars(req)["env"]+"/"+mux.Vars(req)["net"]+"/"+mux.Vars(req)["authenticator"]]
if auth == nil {
mp.respond(res, nil, 404)
} else {
mp.respond(res, auth, 200)
// Next time will return ready
auth.Status = "ready"
}
}

func (mp *mockPlatform) postAuthenticator(res http.ResponseWriter, req *http.Request) {
var auth AuthenticatorAPIModel
mp.getBody(req, &auth)
auth.ID = nanoid.New()
now := time.Now().UTC()
auth.Created = &now
auth.Updated = &now
auth.Status = "pending"
mp.authenticators[mux.Vars(req)["env"]+"/"+mux.Vars(req)["net"]+"/"+auth.ID] = &auth
mp.respond(res, &auth, 201)
}

func (mp *mockPlatform) putAuthenticator(res http.ResponseWriter, req *http.Request) {
auth := mp.authenticators[mux.Vars(req)["env"]+"/"+mux.Vars(req)["net"]+"/"+mux.Vars(req)["authenticator"]]
assert.NotNil(mp.t, auth)
var newAuth AuthenticatorAPIModel
mp.getBody(req, &newAuth)
assert.Equal(mp.t, auth.ID, newAuth.ID) // expected behavior of provider
assert.Equal(mp.t, auth.ID, mux.Vars(req)["authenticator"]) // expected behavior of provider
now := time.Now().UTC()
newAuth.Created = auth.Created
newAuth.Updated = &now
newAuth.Status = "pending"
mp.authenticators[mux.Vars(req)["env"]+"/"+mux.Vars(req)["net"]+"/"+mux.Vars(req)["authenticator"]] = &newAuth
mp.respond(res, &newAuth, 200)
}

func (mp *mockPlatform) deleteAuthenticator(res http.ResponseWriter, req *http.Request) {
rt := mp.authenticators[mux.Vars(req)["env"]+"/"+mux.Vars(req)["net"]+"/"+mux.Vars(req)["authenticator"]]
assert.NotNil(mp.t, rt)
delete(mp.authenticators, mux.Vars(req)["env"]+"/"+mux.Vars(req)["net"]+"/"+mux.Vars(req)["authenticator"])
mp.respond(res, nil, 204)
}
8 changes: 8 additions & 0 deletions kaleido/platform/mockserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type mockPlatform struct {
runtimes map[string]*RuntimeAPIModel
services map[string]*ServiceAPIModel
networks map[string]*NetworkAPIModel
authenticators map[string]*AuthenticatorAPIModel
networkinitdatas map[string]*NetworkInitData
kmsWallets map[string]*KMSWalletAPIModel
kmsKeys map[string]*KMSKeyAPIModel
Expand All @@ -66,6 +67,7 @@ func startMockPlatformServer(t *testing.T) *mockPlatform {
runtimes: make(map[string]*RuntimeAPIModel),
services: make(map[string]*ServiceAPIModel),
networks: make(map[string]*NetworkAPIModel),
authenticators: make(map[string]*AuthenticatorAPIModel),
networkinitdatas: make(map[string]*NetworkInitData),
kmsWallets: make(map[string]*KMSWalletAPIModel),
kmsKeys: make(map[string]*KMSKeyAPIModel),
Expand Down Expand Up @@ -107,6 +109,12 @@ func startMockPlatformServer(t *testing.T) *mockPlatform {
mp.register("/api/v1/environments/{env}/networks/{network}", http.MethodPut, mp.putNetwork)
mp.register("/api/v1/environments/{env}/networks/{network}", http.MethodDelete, mp.deleteNetwork)

// See authenticator_test.go
mp.register("/api/v1/environments/{env}/networks/{net}/authenticators", http.MethodPost, mp.postAuthenticator)
mp.register("/api/v1/environments/{env}/networks/{net}/authenticators/{authenticator}", http.MethodGet, mp.getAuthenticator)
mp.register("/api/v1/environments/{env}/networks/{net}/authenticators/{authenticator}", http.MethodPut, mp.putAuthenticator)
mp.register("/api/v1/environments/{env}/networks/{net}/authenticators/{authenticator}", http.MethodDelete, mp.deleteAuthenticator)

// See network_bootstrap_test.go
mp.register("/api/v1/environments/{env}/networks/{network}/initdata", http.MethodGet, mp.getNetworkInitData)

Expand Down
41 changes: 11 additions & 30 deletions kaleido/platform/network_bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"net/http"
"testing"

"github.com/gorilla/mux"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

_ "embed"
Expand All @@ -30,24 +29,6 @@ data "kaleido_platform_network_bootstrap_data" "boot1" {
}
`

var networkBoostrapStep2 = `
data "kaleido_platform_network_bootstrap_data" "boot1" {
environment = "env1"
network = "net1"
bootstrap_files = jsonencode({
"files": {
"genesis.json": {
"data": {
"text": "{\"alloc\":{\"0x12F62772C4652280d06E64CfBC9033d409559aD4\":{\"balance\":\"0x111111111111\"}},\"coinbase\":\"0x0000000000000000000000000000000000000000\",\"config\":{\"berlinBlock\":0,\"chainId\":12345,\"contractSizeLimit\":98304,\"qbft\":{\"blockperiodseconds\":5,\"epochlength\":30000,\"requesttimeoutseconds\":10},\"shanghaiTime\":0,\"zeroBaseFee\":true},\"difficulty\":\"0x1\",\"extraData\":\"0xf83aa00000000000000000000000000000000000000000000000000000000000000000d594ca45971aa3e3eb66e8cd4ad6ed491520b601bf6cc080c0\",\"gasLimit\":\"0x2fefd800\",\"mixHash\":\"0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365\"}"
},
"type": "json"
}
},
"name": "init"
})
}
`

func TestBootstrap1(t *testing.T) {

mp, providerConfig := testSetup(t)
Expand All @@ -71,21 +52,21 @@ func TestBootstrap1(t *testing.T) {
resource.TestCheckResourceAttr(boot1Data, "environment", `env1`),
),
},
{
Config: providerConfig + networkBoostrapStep2,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(boot1Data, "environment", `env1`),
),
},
},
})
}

func (mp *mockPlatform) getNetworkInitData(res http.ResponseWriter, req *http.Request) {
bd := mp.networkinitdatas[mux.Vars(req)["env"]+"/"+mux.Vars(req)["network"]+"/"+"initdata"]
if bd == nil {
mp.respond(res, nil, 404)
} else {
mp.respond(res, bd, 200)
nid := &NetworkInitData{
Name: "init",
Files: map[string]*FileAPI{
"genesis.json": {
Type: "json",
Data: FileDataAPI{
Text: "{\"alloc\":{\"0x12F62772C4652280d06E64CfBC9033d409559aD4\":{\"balance\":\"0x111111111111\"}},\"coinbase\":\"0x0000000000000000000000000000000000000000\",\"config\":{\"berlinBlock\":0,\"chainId\":12345,\"contractSizeLimit\":98304,\"qbft\":{\"blockperiodseconds\":5,\"epochlength\":30000,\"requesttimeoutseconds\":10},\"shanghaiTime\":0,\"zeroBaseFee\":true},\"difficulty\":\"0x1\",\"extraData\":\"0xf83aa00000000000000000000000000000000000000000000000000000000000000000d59478e9bce6be9b0afa377f29669e8fda460df6838cc080c0\",\"gasLimit\":\"0x2fefd800\",\"mixHash\":\"0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365\"}",
},
},
},
}
mp.respond(res, nid, 200)
}
2 changes: 2 additions & 0 deletions kaleido/platform/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ func TestService1(t *testing.T) {
"GET /api/v1/environments/{env}/services/{service}",
"GET /api/v1/environments/{env}/services/{service}",
"GET /api/v1/environments/{env}/services/{service}",
"GET /api/v1/environments/{env}/services/{service}",
"PUT /api/v1/environments/{env}/services/{service}",
"GET /api/v1/environments/{env}/services/{service}",
"GET /api/v1/environments/{env}/services/{service}",
"GET /api/v1/environments/{env}/services/{service}",
"GET /api/v1/environments/{env}/services/{service}",
"DELETE /api/v1/environments/{env}/services/{service}",
"GET /api/v1/environments/{env}/services/{service}",
})
Expand Down

0 comments on commit 983b9d4

Please sign in to comment.