github.com/therealbill/libredis@v0.0.0-20161227004305-7d50abda5ccf/info/methods_test.go (about)

     1  package info
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  var (
    12  	infostring_raw          string
    13  	infostring_commandstats string
    14  	infostring_keyspace     string
    15  )
    16  
    17  func init() {
    18  	dat, err := ioutil.ReadFile("info-string.txt")
    19  	if err != nil {
    20  		log.Fatal("unable to read file: ", err)
    21  	}
    22  	infostring_raw = string(dat)
    23  
    24  	dat, err = ioutil.ReadFile("commandstats.txt")
    25  	if err != nil {
    26  		log.Fatal("unable to read file: ", err)
    27  	}
    28  	infostring_commandstats = string(dat)
    29  
    30  	dat, err = ioutil.ReadFile("info-string.txt")
    31  	if err != nil {
    32  		log.Fatal("unable to read file: ", err)
    33  	}
    34  	infostring_keyspace = string(dat)
    35  }
    36  
    37  func TestBuildMapFromInfoString(t *testing.T) {
    38  	trimmed := strings.TrimSpace(infostring_raw)
    39  	rmap := BuildMapFromInfoString(trimmed)
    40  	if len(rmap["redis_version"]) == 0 {
    41  		t.Error("Version wasn't parsed")
    42  		t.Fail()
    43  	}
    44  }
    45  
    46  func TestBuildInfoKeyspace(t *testing.T) {
    47  	space := BuildInfoKeyspace(infostring_keyspace)
    48  	if len(space.Databases) == 0 {
    49  		t.Fail()
    50  	}
    51  }
    52  
    53  func TestCommandStats(t *testing.T) {
    54  	stats := CommandStats(infostring_commandstats)
    55  	if len(stats.Stats) == 0 {
    56  		t.Fail()
    57  	}
    58  	// The act of calling CommandStats will produce at least one call it info
    59  	// So we ensure we have at least one call
    60  	if stats.Stats["info"]["calls"] == 0 {
    61  		t.Fail()
    62  	}
    63  }
    64  
    65  func TestKeyspaceStats(t *testing.T) {
    66  	stats := KeyspaceStats(infostring_keyspace)
    67  	if len(stats.Databases) == 0 {
    68  		fmt.Printf("%+v\n", stats)
    69  		t.Error("didn't get expected Keyspace Stats structure.")
    70  		t.Fail()
    71  	}
    72  }
    73  
    74  func TestBuildAllInfoMap(t *testing.T) {
    75  	alldata := BuildAllInfoMap(infostring_raw)
    76  	if len(alldata["CPU"]["used_cpu_sys"]) == 0 {
    77  		fmt.Printf("alldata.cpu.used_cpu_sys: %+v\n", alldata["CPU"]["used_cpu_sys"])
    78  		t.Error("didn't parse cpu.used_cpu_sys")
    79  		t.Fail()
    80  	}
    81  }
    82  
    83  func TestGetAllInfo(t *testing.T) {
    84  	all := GetAllInfo(infostring_raw)
    85  	// Server Checks
    86  	if all.Server.Arch_bits == 0 {
    87  		t.Error("Didn't parse Server.Arch_bits")
    88  		t.Fail()
    89  	}
    90  }
    91  
    92  func TestInfo(t *testing.T) {
    93  	all := GetAllInfo(infostring_raw)
    94  	// Server Checks
    95  	if all.Server.Arch_bits == 0 {
    96  		t.Error("Didn't parse Server.Arch_bits")
    97  		t.Fail()
    98  	}
    99  	if len(all.Server.Version) == 0 {
   100  		t.Error("Didn't parse Server.Version")
   101  		t.Fail()
   102  	}
   103  	// Replication Checks
   104  	if len(all.Replication.Role) == 0 {
   105  		t.Error("Failed to parse Replication.Role")
   106  		t.Fail()
   107  	}
   108  
   109  	// Persistence
   110  	if all.Persistence.AOFEnabled {
   111  		t.Error("Tests assume default config, so this AOFEnabled shoudl be false")
   112  		t.Fail()
   113  	}
   114  	// Stats
   115  	if all.Stats.TotalCommandsProcessed == 0 {
   116  		t.Error("Failed to parse Stats.TotalCommandsProcessed")
   117  		t.Fail()
   118  	}
   119  	// Memory
   120  	if len(all.Memory.UsedMemoryHuman) == 0 {
   121  		t.Error("Failed to parse Memory.UsedMemoryHuman")
   122  		t.Fail()
   123  	}
   124  	if all.Memory.UsedMemory == 0 {
   125  		t.Error("Failed to parse Memory.UsedMemory")
   126  		t.Fail()
   127  	}
   128  	// Keyspace
   129  	if len(all.Keyspace.Databases) == 0 {
   130  		t.Error("Failed to parse at least one DB from Keyspace")
   131  		t.Fail()
   132  	}
   133  	if all.Commandstats.Stats["info"]["calls"] == 0 {
   134  		t.Error("Failed to parse stats on info command from Commandstats")
   135  		fmt.Printf("Commandstats:\t%+v\n", all.Commandstats)
   136  		t.Fail()
   137  	}
   138  
   139  }
   140  
   141  func TestUpperFirst(t *testing.T) {
   142  	instring := "this"
   143  	outsting := upperFirst(instring)
   144  	if instring == outsting {
   145  		t.Error("Failed to convert this to This")
   146  		t.Fail()
   147  	}
   148  	if outsting != "This" {
   149  		t.Error("Failed to convert this to This")
   150  		t.Fail()
   151  	}
   152  
   153  	empty := upperFirst("")
   154  	if empty != "" {
   155  		t.Error("upperFirst on empty strign result sin non-empty string")
   156  		t.Fail()
   157  	}
   158  }