Skip to content
Snippets Groups Projects
chain_test.go 2.79 KiB
Newer Older
AlexStocks's avatar
AlexStocks committed
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
fangyincheng's avatar
fangyincheng committed

邹毅贤's avatar
邹毅贤 committed
package chain
邹毅贤's avatar
邹毅贤 committed

import (
	"context"
邹毅贤's avatar
邹毅贤 committed
	"github.com/apache/dubbo-go/cluster/router/condition"
邹毅贤's avatar
邹毅贤 committed
	"github.com/apache/dubbo-go/common"
邹毅贤's avatar
邹毅贤 committed
	"github.com/apache/dubbo-go/common/config"
邹毅贤's avatar
邹毅贤 committed
	"github.com/apache/dubbo-go/common/extension"
	_ "github.com/apache/dubbo-go/config_center/zookeeper"
	"github.com/apache/dubbo-go/remoting/zookeeper"
	"github.com/stretchr/testify/assert"
邹毅贤's avatar
邹毅贤 committed
	"strconv"
邹毅贤's avatar
邹毅贤 committed
	"testing"
	"time"
)

import (
	_ "github.com/apache/dubbo-go/cluster/router/condition"
)

func TestNewRouterChain(t *testing.T) {
	ts, z, _, err := zookeeper.NewMockZookeeperClient("test", 15*time.Second)
	assert.NoError(t, err)
邹毅贤's avatar
邹毅贤 committed
	err = z.Create("/dubbo/config/dubbo/test-condition.condition-router")
	assert.NoError(t, err)

邹毅贤's avatar
邹毅贤 committed
	testyml := `enabled: true
邹毅贤's avatar
邹毅贤 committed
force: true
runtime: false
conditions:
  - => host != 172.22.3.91
`

邹毅贤's avatar
邹毅贤 committed
	_, err = z.Conn.Set("/dubbo/config/dubbo/test-condition.condition-router", []byte(testyml), 0)
邹毅贤's avatar
邹毅贤 committed
	assert.NoError(t, err)
邹毅贤's avatar
邹毅贤 committed
	defer ts.Stop()
	defer z.Close()

邹毅贤's avatar
邹毅贤 committed
	//t.Log(z.Conn.Server())
邹毅贤's avatar
邹毅贤 committed

邹毅贤's avatar
邹毅贤 committed
	zkUrl, _ := common.NewURL(context.TODO(), "zookeeper://127.0.0.1:"+strconv.Itoa(ts.Servers[0].Port))
邹毅贤's avatar
邹毅贤 committed
	configuration, err := extension.GetConfigCenterFactory("zookeeper").GetDynamicConfiguration(&zkUrl)
邹毅贤's avatar
邹毅贤 committed
	config.GetEnvInstance().SetDynamicConfiguration(configuration)

邹毅贤's avatar
邹毅贤 committed
	assert.Nil(t, err)
	assert.NotNil(t, configuration)

邹毅贤's avatar
邹毅贤 committed
	chain := NewRouterChain(getRouteUrl("test-condition"))
邹毅贤's avatar
邹毅贤 committed
	assert.Equal(t, 1, len(chain.routers))
	appRouter := chain.routers[0].(*condition.AppRouter)

	assert.NotNil(t, appRouter)
	assert.NotNil(t, appRouter.RouterRule())
	rule := appRouter.RouterRule()
	assert.Equal(t, "", rule.Scope)
	assert.True(t, rule.Force)
	assert.True(t, rule.Enabled)
	assert.True(t, rule.Valid)

	assert.Equal(t, testyml, rule.RawRule)
	assert.Equal(t, false, rule.Runtime)
	assert.Equal(t, false, rule.Dynamic)
	assert.Equal(t, "", rule.Key)
邹毅贤's avatar
邹毅贤 committed
}

func getRouteUrl(applicationKey string) *common.URL {
	url, _ := common.NewURL(context.TODO(), "condition://0.0.0.0/com.foo.BarService")
	url.AddParam("application", applicationKey)
	url.AddParam("force", "true")
	return &url
}