github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/server/resources/resources_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 resources_test
    17  
    18  import (
    19  	"os"
    20  	"testing"
    21  
    22  	. "github.com/onsi/ginkgo"
    23  	. "github.com/onsi/gomega"
    24  
    25  	"github.com/cloudwan/gohan/db"
    26  	"github.com/cloudwan/gohan/db/transaction"
    27  	"github.com/cloudwan/gohan/schema"
    28  	"github.com/cloudwan/gohan/util"
    29  )
    30  
    31  const (
    32  	configDir         = ".."
    33  	configFile        = "./server_test_config.yaml"
    34  	dbType            = "sqlite3"
    35  	dbFile            = "./test.db"
    36  	adminTokenID      = "admin_token"
    37  	memberTokenID     = "member_token"
    38  	powerUserTokenID  = "power_user_token"
    39  	adminTenantID     = "fc394f2ab2df4114bde39905f800dc57"
    40  	memberTenantID    = "fc394f2ab2df4114bde39905f800dc57"
    41  	powerUserTenantID = "acf5662bbff44060b93ac3db3c25a590"
    42  )
    43  
    44  var (
    45  	testDB    db.DB
    46  	whitelist = map[string]bool{
    47  		"schema":    true,
    48  		"policy":    true,
    49  		"extension": true,
    50  		"namespace": true,
    51  	}
    52  )
    53  
    54  func clearTable(tx transaction.Transaction, s *schema.Schema) error {
    55  	for _, schema := range schema.GetManager().Schemas() {
    56  		if schema.ParentSchema == s {
    57  			err := clearTable(tx, schema)
    58  			if err != nil {
    59  				return err
    60  			}
    61  		}
    62  	}
    63  	resources, _, err := tx.List(s, nil, nil)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	for _, resource := range resources {
    68  		err = tx.Delete(s, resource.ID())
    69  		if err != nil {
    70  			return err
    71  		}
    72  	}
    73  	return nil
    74  }
    75  
    76  func TestResources(t *testing.T) {
    77  	RegisterFailHandler(Fail)
    78  	RunSpecs(t, "Resources Suite")
    79  }
    80  
    81  var _ = Describe("Suit set up and tear down", func() {
    82  	var _ = BeforeSuite(func() {
    83  		var err error
    84  		Expect(os.Chdir(configDir)).To(Succeed())
    85  		testDB, err = db.ConnectDB(dbType, dbFile)
    86  		Expect(err).ToNot(HaveOccurred(), "Failed to connect database.")
    87  		manager := schema.GetManager()
    88  		config := util.GetConfig()
    89  		Expect(config.ReadConfig(configFile)).To(Succeed())
    90  		schemaFiles := config.GetStringList("schemas", nil)
    91  		Expect(schemaFiles).NotTo(BeNil())
    92  		Expect(manager.LoadSchemasFromFiles(schemaFiles...)).To(Succeed())
    93  		Expect(db.InitDBWithSchemas(dbType, dbFile, false, false)).To(Succeed())
    94  	})
    95  
    96  	var _ = AfterSuite(func() {
    97  		schema.ClearManager()
    98  		os.Remove(dbFile)
    99  	})
   100  })