This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
bensolo-io
committed
Jul 5, 2023
1 parent
af79755
commit 30824ef
Showing
7 changed files
with
255 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
package plugin | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1" | ||
rolloutsPlugin "github.com/argoproj/argo-rollouts/rollout/trafficrouting/plugin/rpc" | ||
"github.com/bensolo-io/rollouts-plugin-trafficrouter-glooplatform/pkg/mocks" | ||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
goPlugin "github.com/hashicorp/go-plugin" | ||
) | ||
|
||
var testHandshake = goPlugin.HandshakeConfig{ | ||
ProtocolVersion: 1, | ||
MagicCookieKey: "ARGO_ROLLOUTS_RPC_PLUGIN", | ||
MagicCookieValue: "trafficrouter", | ||
} | ||
|
||
// func TestRollouts(t *testing.T) { | ||
|
||
// err := filepath.Walk("testfiles", | ||
// func(path string, info os.FileInfo, err error) error { | ||
// if err != nil { | ||
// return err | ||
// } | ||
|
||
// if !info.IsDir() && filepath.Ext(path) == ".yaml" { | ||
// data, err := ioutil.ReadFile(path) | ||
// if err != nil { | ||
// return fmt.Errorf("failed to read bytes from '%s': %s", path, err) | ||
// } | ||
// } | ||
|
||
// return nil | ||
// }) | ||
|
||
// assert.Empty(t, err) | ||
// } | ||
|
||
func TestRunSuccessfully(t *testing.T) { | ||
logCtx := log.WithFields(log.Fields{"plugin": "trafficrouter"}) | ||
log.SetLevel(log.DebugLevel) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
// this is a better way, but mock clientset is missing ExternalEndpoints func | ||
// mockClient := mocknetworkv2.NewMockRouteTableClient(gomock.NewController(t)) //.GetRouteTable(ctx, &mocks.RouteTable) | ||
|
||
rpcPluginImp := &RpcPlugin{ | ||
LogCtx: logCtx, | ||
MockRouteTable: &mocks.RouteTable, | ||
} | ||
|
||
// pluginMap is the map of plugins we can dispense. | ||
var pluginMap = map[string]goPlugin.Plugin{ | ||
"RpcTrafficRouterPlugin": &rolloutsPlugin.RpcTrafficRouterPlugin{Impl: rpcPluginImp}, | ||
} | ||
|
||
ch := make(chan *goPlugin.ReattachConfig, 1) | ||
closeCh := make(chan struct{}) | ||
go goPlugin.Serve(&goPlugin.ServeConfig{ | ||
HandshakeConfig: testHandshake, | ||
Plugins: pluginMap, | ||
Test: &goPlugin.ServeTestConfig{ | ||
Context: ctx, | ||
ReattachConfigCh: ch, | ||
CloseCh: closeCh, | ||
}, | ||
}) | ||
|
||
// We should get a config | ||
var config *goPlugin.ReattachConfig | ||
select { | ||
case config = <-ch: | ||
case <-time.After(2000 * time.Millisecond): | ||
t.Fatal("should've received reattach") | ||
} | ||
if config == nil { | ||
t.Fatal("config should not be nil") | ||
} | ||
|
||
// Connect! | ||
c := goPlugin.NewClient(&goPlugin.ClientConfig{ | ||
Cmd: nil, | ||
HandshakeConfig: testHandshake, | ||
Plugins: pluginMap, | ||
Reattach: config, | ||
}) | ||
client, err := c.Client() | ||
if err != nil { | ||
t.Fatalf("err: %s", err) | ||
} | ||
|
||
// Pinging should work | ||
if err := client.Ping(); err != nil { | ||
t.Fatalf("should not err: %s", err) | ||
} | ||
|
||
// Kill which should do nothing | ||
c.Kill() | ||
if err := client.Ping(); err != nil { | ||
t.Fatalf("should not err: %s", err) | ||
} | ||
|
||
// Request the plugin | ||
raw, err := client.Dispense("RpcTrafficRouterPlugin") | ||
if err != nil { | ||
t.Fail() | ||
} | ||
|
||
pluginInstance := raw.(*rolloutsPlugin.TrafficRouterPluginRPC) | ||
err = pluginInstance.InitPlugin() | ||
if err.Error() != "" { | ||
t.Fail() | ||
} | ||
t.Run("SetWeight", func(t *testing.T) { | ||
var desiredWeight int32 = 30 | ||
err := pluginInstance.SetWeight(newMockRollout(), desiredWeight, []v1alpha1.WeightDestination{}) | ||
|
||
assert.Empty(t, err.Error()) | ||
assert.Equal(t, uint32(100-desiredWeight), mocks.RouteTable.Spec.Http[0].GetForwardTo().Destinations[0].Weight) | ||
assert.Equal(t, uint32(desiredWeight), mocks.RouteTable.Spec.Http[0].GetForwardTo().Destinations[1].Weight) | ||
}) | ||
|
||
// Canceling should cause an exit | ||
cancel() | ||
<-closeCh | ||
} | ||
|
||
func newMockRollout() *v1alpha1.Rollout { | ||
pluginConfig := GlooPlatformAPITrafficRouting{ | ||
RouteTableName: mocks.RouteTableName, | ||
RouteTableNamespace: mocks.RouteTableNamespace, | ||
DestinationKind: mocks.DestinationKind, | ||
DestinationNamespace: mocks.DestinationNamespace, | ||
} | ||
|
||
encoded, err := json.Marshal(pluginConfig) | ||
if err != nil { | ||
log.Fatalf("failed to marshal plugin config: %s", err) | ||
} | ||
|
||
return &v1alpha1.Rollout{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: mocks.RolloutName, | ||
Namespace: mocks.RolloutNamespace, | ||
}, | ||
Spec: v1alpha1.RolloutSpec{ | ||
Strategy: v1alpha1.RolloutStrategy{ | ||
Canary: &v1alpha1.CanaryStrategy{ | ||
StableService: mocks.StableService, | ||
CanaryService: mocks.CanaryService, | ||
TrafficRouting: &v1alpha1.RolloutTrafficRouting{ | ||
Plugins: map[string]json.RawMessage{ | ||
"solo-io/glooplatformAPI": encoded, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
Oops, something went wrong.