github.com/mmatczuk/gohan@v0.0.0-20170206152520-30e45d9bdb69/extension/otto/otto_suite_test.go (about)

     1  // Copyright (C) 2015 NTT Innovation Institute, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package otto_test
    17  
    18  import (
    19  	"os"
    20  	"testing"
    21  
    22  	"github.com/cloudwan/gohan/db"
    23  	"github.com/cloudwan/gohan/db/transaction"
    24  	"github.com/cloudwan/gohan/schema"
    25  	"github.com/cloudwan/gohan/util"
    26  
    27  	. "github.com/onsi/ginkgo"
    28  	. "github.com/onsi/gomega"
    29  	"github.com/cloudwan/gohan/sync/etcdv3"
    30  	"time"
    31  )
    32  
    33  const (
    34  	configDir         = "../../server"
    35  	configFile        = "./server_test_config.yaml"
    36  	dbType            = "sqlite3"
    37  	dbFile            = "./test.db"
    38  	testSyncEndpoint = "localhost:2379"
    39  	adminTokenID      = "admin_token"
    40  	memberTokenID     = "demo_token"
    41  	powerUserTokenID  = "power_user_token"
    42  	adminTenantID     = "fc394f2ab2df4114bde39905f800dc57"
    43  	memberTenantID    = "fc394f2ab2df4114bde39905f800dc57"
    44  	powerUserTenantID = "acf5662bbff44060b93ac3db3c25a590"
    45  )
    46  
    47  var (
    48  	testDB    db.DB
    49  	testSync *etcdv3.Sync
    50  
    51  	whitelist = map[string]bool{
    52  		"schema":    true,
    53  		"policy":    true,
    54  		"extension": true,
    55  		"namespace": true,
    56  	}
    57  )
    58  
    59  func clearTable(tx transaction.Transaction, s *schema.Schema) error {
    60  	if s.IsAbstract() {
    61  		return nil
    62  	}
    63  	for _, schema := range schema.GetManager().Schemas() {
    64  		if schema.ParentSchema == s {
    65  			err := clearTable(tx, schema)
    66  			if err != nil {
    67  				return err
    68  			}
    69  		}
    70  	}
    71  	resources, _, err := tx.List(s, nil, nil)
    72  	if err != nil {
    73  		return err
    74  	}
    75  	for _, resource := range resources {
    76  		err = tx.Delete(s, resource.ID())
    77  		if err != nil {
    78  			return err
    79  		}
    80  	}
    81  	return nil
    82  }
    83  
    84  func TestOttoExtension(t *testing.T) {
    85  	RegisterFailHandler(Fail)
    86  	RunSpecs(t, "Otto Extension Suite")
    87  }
    88  
    89  var _ = Describe("Suite set up and tear down", func() {
    90  	var _ = BeforeSuite(func() {
    91  		var err error
    92  		Expect(os.Chdir(configDir)).To(Succeed())
    93  		testDB, err = db.ConnectDB(dbType, dbFile, db.DefaultMaxOpenConn)
    94  		Expect(err).ToNot(HaveOccurred(), "Failed to connect database.")
    95  		testSync, err = etcdv3.NewSync([]string{testSyncEndpoint}, time.Second)
    96  		Expect(err).NotTo(HaveOccurred(), "Failed to connect to etcd")
    97  		manager := schema.GetManager()
    98  		config := util.GetConfig()
    99  		Expect(config.ReadConfig(configFile)).To(Succeed())
   100  		schemaFiles := config.GetStringList("schemas", nil)
   101  		Expect(schemaFiles).NotTo(BeNil())
   102  		Expect(manager.LoadSchemasFromFiles(schemaFiles...)).To(Succeed())
   103  		Expect(db.InitDBWithSchemas(dbType, dbFile, false, false)).To(Succeed())
   104  	})
   105  
   106  	var _ = AfterSuite(func() {
   107  		schema.ClearManager()
   108  		testSync.Close()
   109  		os.Remove(dbFile)
   110  	})
   111  })