github.com/confluentinc/confluent-kafka-go@v1.9.2/schemaregistry/testhelpers.go (about)

     1  /**
     2   * Copyright 2022 Confluent Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package schemaregistry
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"os"
    23  	"reflect"
    24  	"runtime"
    25  	"testing"
    26  )
    27  
    28  type testConf map[string]interface{}
    29  type failFunc func(string, ...error)
    30  
    31  var testconf = make(testConf)
    32  var srClient Client
    33  var maybeFail failFunc
    34  
    35  // NewTestConf reads the test suite config file testconf.json which must
    36  // contain at least Brokers and Topic string properties.
    37  // Returns Testconf if the testconf was found and usable,
    38  // error if file can't be read correctly
    39  func testconfRead() bool {
    40  	cf, err := os.Open("../kafka/testconf.json")
    41  	defer cf.Close()
    42  	if err != nil {
    43  		fmt.Fprintf(os.Stderr, "%s testconf.json not found - ignoring test\n", err)
    44  		return false
    45  	}
    46  
    47  	jp := json.NewDecoder(cf)
    48  	err = jp.Decode(&testconf)
    49  
    50  	if err != nil {
    51  		panic(fmt.Sprintf("Failed to parse testconf: %s", err))
    52  	}
    53  
    54  	return true
    55  }
    56  
    57  // getObject returns a child object of the root testConf
    58  func (tc testConf) getObject(name string) testConf {
    59  	return tc[name].(map[string]interface{})
    60  }
    61  
    62  // getString returns a string representation of the value represented by key from the provided namespace
    63  // if the namespace is an empty string the root object will be searched.
    64  func (tc testConf) getString(key string) string {
    65  	val, ok := tc[key]
    66  	if ok {
    67  		return val.(string)
    68  	}
    69  	return ""
    70  }
    71  
    72  // getInt returns an integer representation of the value represented by key from the provided namespace
    73  // If the namespace is an empty string the root object will be searched.
    74  func (tc testConf) getInt(key string) int {
    75  	val, ok := tc[key]
    76  	if ok {
    77  		return val.(int)
    78  	}
    79  	return 0
    80  }
    81  
    82  func initFailFunc(t *testing.T) failFunc {
    83  	tester := t
    84  	return func(msg string, errors ...error) {
    85  		for _, err := range errors {
    86  			if err != nil {
    87  				pc := make([]uintptr, 1)
    88  				runtime.Callers(2, pc)
    89  				caller := runtime.FuncForPC(pc[0])
    90  				_, line := caller.FileLine(caller.Entry())
    91  
    92  				tester.Fatalf("%s:%d failed: %s %s", caller.Name(), line, msg, err)
    93  			}
    94  		}
    95  	}
    96  }
    97  
    98  func expect(actual, expected interface{}) error {
    99  	if !reflect.DeepEqual(actual, expected) {
   100  		return fmt.Errorf("expected: %v, Actual: %v", expected, actual)
   101  	}
   102  
   103  	return nil
   104  }