func TestDeliveryServiceState(t *testing.T) {
	resp := fixtures.DeliveryServiceState()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for a DeliveryServiceState")

	state, err := to.DeliveryServiceState("123")
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if state.Failover.Destination.Location != "someLocation" {
		testHelper.Error(t, "Should get back \"someLocation\" for \"Failover.Destination.Location\", got: %s", state.Failover.Destination.Location)
	} else {
		testHelper.Success(t, "Should get back \"someLocation\" for \"Failover.Destination.Location\"")
	}

	if state.Enabled != true {
		testHelper.Error(t, "Should get back \"true\" for \"Enabled\", got: %s", state.Enabled)
	} else {
		testHelper.Success(t, "Should get back \"true\" for \"Enabled\"")
	}
}
예제 #2
0
// getDeliveryServiceServers gets the servers on each delivery services, for the given CDN, from Traffic Ops.
// Returns a map[deliveryService][]server, and a map[server]deliveryService
func getDeliveryServiceServers(to *traffic_ops.Session, cdn string) (map[string][]string, map[string]string, error) {
	dsServers := map[string][]string{}
	serverDs := map[string]string{}

	crcData, err := to.CRConfigRaw(cdn)
	if err != nil {
		return nil, nil, err
	}
	type CrConfig struct {
		ContentServers map[string]struct {
			DeliveryServices map[string][]string `json:"deliveryServices"`
		} `json:"contentServers"`
	}
	var crc CrConfig
	if err := json.Unmarshal(crcData, &crc); err != nil {
		return nil, nil, err
	}

	for serverName, serverData := range crc.ContentServers {
		for deliveryServiceName, _ := range serverData.DeliveryServices {
			dsServers[deliveryServiceName] = append(dsServers[deliveryServiceName], serverName)
			serverDs[serverName] = deliveryServiceName
		}
	}
	return dsServers, serverDs, nil
}
예제 #3
0
func TestTypes(t *testing.T) {
	resp := fixtures.Types()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for Types")

	types, err := to.Types()
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	for _, n := range types {
		if n.Name != "EDGE" {
			testHelper.Error(t, "Should get back \"EDGE\" for \"Name\", got %s", n.Name)
		} else {
			testHelper.Success(t, "Should get back \"EDGE\" for \"Name\"")
		}

		if n.Description != "edge cache" {
			testHelper.Error(t, "Should get back \"edge cache\" for \"Description\", got %s", n.Description)
		} else {
			testHelper.Success(t, "Should get back \"edge cache\" for \"Description\"")
		}
	}
}
예제 #4
0
func getDeliveryServiceTypes(to *traffic_ops.Session, cdn string) (map[string]deliveryservicestats.DsStatType, error) {
	dsTypes := map[string]deliveryservicestats.DsStatType{}

	crcData, err := to.CRConfigRaw(cdn)
	if err != nil {
		return nil, err
	}
	type CrConfig struct {
		DeliveryServices map[string]struct {
			Matchsets []struct {
				Protocol string `json:"protocol"`
			} `json:"matchsets"`
		} `json:"deliveryServices"`
	}
	var crc CrConfig
	if err := json.Unmarshal(crcData, &crc); err != nil {
		return nil, fmt.Errorf("Error unmarshalling CRConfig: %v", err)
	}

	for dsName, dsData := range crc.DeliveryServices {
		if len(dsData.Matchsets) < 1 {
			return nil, fmt.Errorf("CRConfig missing protocol for '%s'", dsName)
		}
		dsTypeStr := dsData.Matchsets[0].Protocol
		dsType := deliveryservicestats.DsStatTypeFromString(dsTypeStr)
		if dsType == deliveryservicestats.DsStatTypeInvalid {
			return nil, fmt.Errorf("CRConfig unknowng protocol for '%s': '%s'", dsName, dsTypeStr)
		}
		dsTypes[dsName] = dsType
	}
	return dsTypes, nil
}
예제 #5
0
func TestServerFQDN(t *testing.T) {
	resp := fixtures.Servers()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	shortName := "edge-alb-01"
	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for the FQDN of Server: \"%s\"", shortName)

	s, err := to.ServersFqdn("edge-alb-01")
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if s != "edge-alb-01.albuquerque.nm.albuq.kabletown.com" {
		testHelper.Error(t, "Should get back \"edge-alb-01.albuquerque.nm.albuq.kabletown.com\", got: %s", s)
	} else {
		testHelper.Success(t, "Should get back \"edge-alb-01.albuquerque.nm.albuq.kabletown.com\"")
	}
}
예제 #6
0
func TestServerShortName(t *testing.T) {
	resp := fixtures.Servers()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	pattern := "edge"
	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for servers that match Short Name: \"%s\"", pattern)

	servers, err := to.ServersShortNameSearch(pattern)
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if servers[0] != "edge-alb-01" {
		testHelper.Error(t, "Should get back \"edge-alb-01\", got: %s", servers[0])
	} else {
		testHelper.Success(t, "Should get back \"edge-alb-01\"")
	}

	if servers[1] != "edge-alb-02" {
		testHelper.Error(t, "Should get back \"edge-alb-02\", got: %s", servers[1])
	} else {
		testHelper.Success(t, "Should get back \"edge-alb-02\"")
	}
}
func TestDeliveryServiceRouting(t *testing.T) {
	resp := fixtures.DeliveryServiceRouting()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for a DeliveryServiceRouting")

	routing, err := to.DeliveryServiceRouting("123")
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if routing.StaticRoute != 1 {
		testHelper.Error(t, "Should get back \"1\" for \"StaticRoute\", got: %s", routing.StaticRoute)
	} else {
		testHelper.Success(t, "Should get back \"1\" for \"StaticRoute\"")
	}
}
func TestDeliveryService(t *testing.T) {
	resp := fixtures.DeliveryServices()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for a DeliveryService")

	ds, err := to.DeliveryService("123")
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if ds.XMLID != "ds-test" {
		testHelper.Error(t, "Should get back \"ds-test\" for \"XMLID\", got: %s", ds.XMLID)
	} else {
		testHelper.Success(t, "Should get back \"ds-test\" for \"XMLID\"")
	}

	if ds.MissLong != "-99.123456" {
		testHelper.Error(t, "Should get back \"-99.123456\" for \"MissLong\", got: %s", ds.MissLong)
	} else {
		testHelper.Success(t, "Should get back \"-99.123456\" for \"MissLong\"")
	}
}
예제 #9
0
// getServerTypes gets the cache type of each ATS Edge+Mid Cache server, for the given CDN, from Traffic Ops.
func getServerTypes(to *traffic_ops.Session, cdn string) (map[string]deliveryservicestats.DsStatCacheType, error) {
	serverTypes := map[string]deliveryservicestats.DsStatCacheType{}

	crcData, err := to.CRConfigRaw(cdn)
	if err != nil {
		return nil, err
	}
	type CrConfig struct {
		ContentServers map[string]struct {
			Type string `json:"type"`
		} `json:"contentServers"`
	}
	var crc CrConfig
	if err := json.Unmarshal(crcData, &crc); err != nil {
		return nil, err
	}

	for server, serverData := range crc.ContentServers {
		t := deliveryservicestats.DsStatCacheTypeFromString(serverData.Type)
		if t == deliveryservicestats.DsStatCacheTypeInvalid {
			return nil, fmt.Errorf("getServerTypes CRConfig unknown type for '%s': '%s'", server, serverData.Type)
		}
		serverTypes[server] = t
	}
	return serverTypes, nil
}
예제 #10
0
// getDeliveryServiceRegexes gets the regexes of each delivery service, for the given CDN, from Traffic Ops.
// Returns a map[deliveryService][]regex.
func getDeliveryServiceRegexes(to *traffic_ops.Session, cdn string) (map[string][]string, error) {
	dsRegexes := map[string][]string{}

	crcData, err := to.CRConfigRaw(cdn)
	if err != nil {
		return nil, err
	}
	type CrConfig struct {
		DeliveryServices map[string]struct {
			Matchsets []struct {
				MatchList []struct {
					Regex string `json:"regex"`
				} `json:"matchlist"`
			} `json:"matchsets"`
		} `json:"deliveryServices"`
	}
	var crc CrConfig
	if err := json.Unmarshal(crcData, &crc); err != nil {
		return nil, err
	}

	for dsName, dsData := range crc.DeliveryServices {
		if len(dsData.Matchsets) < 1 {
			return nil, fmt.Errorf("CRConfig missing regex for '%s'", dsName)
		}
		for _, matchset := range dsData.Matchsets {
			if len(matchset.MatchList) < 1 {
				return nil, fmt.Errorf("CRConfig missing Regex for '%s'", dsName)
			}
			dsRegexes[dsName] = append(dsRegexes[dsName], matchset.MatchList[0].Regex)
		}
	}
	return dsRegexes, nil
}
예제 #11
0
func getServerTypes(to *traffic_ops.Session, cdn string) (map[string]string, error) {
	// This is efficient (with getDeliveryServiceServers) because the traffic_ops client caches its result.
	// Were that not the case, these functions could be refactored to only call traffic_ops.Session.CRConfigRaw() once.

	crcData, err := to.CRConfigRaw(cdn)
	if err != nil {
		return nil, err
	}

	type CrConfig struct {
		ContentServers map[string]struct {
			Type string `json:"type"`
		} `json:"contentServers"`
	}
	var crc CrConfig
	if err := json.Unmarshal(crcData, &crc); err != nil {
		return nil, err
	}

	serverTypes := map[string]string{}
	for serverName, serverData := range crc.ContentServers {
		serverTypes[serverName] = serverData.Type
	}
	return serverTypes, nil
}
예제 #12
0
func TestUsers(t *testing.T) {
	resp := fixtures.Users()
	server := testHelper.ValidHTTPServer(resp.Response)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for Users")

	users, err := to.Users()
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	for _, u := range users {
		if u.FullName != "Bob Smith" {
			testHelper.Error(t, "Should get back \"Bob Smith\" for \"FullName\", got %s", u.FullName)
		} else {
			testHelper.Success(t, "Should get back \"Bob Smith\" for \"FullName\"")
		}

		if u.PublicSSHKey != "some-ssh-key" {
			testHelper.Error(t, "Should get back \"some-ssh-key\" for \"PublicSSHKey\", got %s", u.PublicSSHKey)
		} else {
			testHelper.Success(t, "Should get back \"some-ssh-key\" for \"PublicSSHKey\"")
		}

		if u.Role != "3" {
			testHelper.Error(t, "Should get back \"3\" for \"Role\", got %s", u.Role)
		} else {
			testHelper.Success(t, "Should get back \"3\" for \"Role\"")
		}

		if u.Email != "*****@*****.**" {
			testHelper.Error(t, "Should get back \"[email protected]\" for \"Email\", got %s", u.Email)
		} else {
			testHelper.Success(t, "Should get back \"[email protected]\" for \"Email\"")
		}

		if u.Username != "bsmith" {
			testHelper.Error(t, "Should get back \"bsmith\" for \"Username\", got %s", u.Username)
		} else {
			testHelper.Success(t, "Should get back \"bsmith\" for \"Username\"")
		}
	}
}
예제 #13
0
func TestCacheGroup(t *testing.T) {
	resp := fixtures.Cachegroups()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for CacheGroups")

	cacheGroups, err := to.CacheGroups()
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if len(cacheGroups) != 1 {
		testHelper.Error(t, "Should get back \"1\" CacheGroups, got: %d", len(cacheGroups))
	} else {
		testHelper.Success(t, "Should get back \"1\" CacheGroups")
	}

	for _, cacheGroup := range cacheGroups {
		if cacheGroup.Name == "" {
			testHelper.Error(t, "Should get back \"edge-philadelphia\" for \"Name\", got: %s", cacheGroup.Name)
		} else {
			testHelper.Success(t, "Should get back \"edge-philadelphia\" for \"Name\"")
		}

		if cacheGroup.Longitude != 5 {
			testHelper.Error(t, "Should get back \"5\" for \"Longitude\", got: %v", cacheGroup.Longitude)
		} else {
			testHelper.Success(t, "Should get back \"5\" for \"Longitude\"")
		}

		if cacheGroup.Latitude != 55 {
			testHelper.Error(t, "Should get back \"55\" for \"Latitude\", got: %v", cacheGroup.Latitude)
		} else {
			testHelper.Success(t, "Should get back \"55\" for \"Latitude\"")
		}

		if cacheGroup.ParentName != "mid-northeast" {
			testHelper.Error(t, "Should get back \"mid-northeast\" for \"ParentName\", got: %s", cacheGroup.ParentName)
		} else {
			testHelper.Success(t, "Should get back \"mid-northeast\" for \"ParentName\"")
		}
	}
}
func TestStatsSummary(t *testing.T) {
	resp := fixtures.StatsSummary()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for Stats Summary")

	stats, err := to.SummaryStats("test-cdn", "test-ds1", "test-stat")
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if len(stats) != 1 {
		testHelper.Error(t, "Should get back \"1\" Parameter, got: %d", len(stats))
	} else {
		testHelper.Success(t, "Should get back \"1\" Parameter")
	}

	for _, s := range stats {
		if s.StatName != "test-stat" {
			testHelper.Error(t, "Should get back \"test-stat\" for \"StatName\", got: %s", s.StatName)
		} else {
			testHelper.Success(t, "Should get back \"test-stat\" for \"StatName\"")
		}

		if s.DeliveryService != "test-ds1" {
			testHelper.Error(t, "Should get back \"test-ds1\" for \"DeliveryService\", got: %s", s.DeliveryService)
		} else {
			testHelper.Success(t, "Should get back \"test-ds1\" for \"DeliveryService\"")
		}

		if s.StatValue != "3.1415" {
			testHelper.Error(t, "Should get back \"3.1415\" for \"StatValue\", got: %s", s.StatValue)
		} else {
			testHelper.Success(t, "Should get back \"3.1415\" for \"StatValue\"")
		}

		if s.CDNName != "test-cdn" {
			testHelper.Error(t, "Should get back \"test-cdn\" for \"CDNName\", got: %s", s.CDNName)
		} else {
			testHelper.Success(t, "Should get back \"test-cdn\" for \"CDNName\"")
		}
	}
}
예제 #15
0
func TestServerByType(t *testing.T) {
	resp := fixtures.LogstashServers()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for \"Logstash\" Servers")

	params := make(url.Values)
	params.Add("type", "Logstash")

	servers, err := to.ServersByType(params)
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if len(servers) != 2 {
		testHelper.Error(t, "Should get back \"2\" Server, got: %d", len(servers))
	} else {
		testHelper.Success(t, "Should get back \"2\" Server")
	}

	if servers[0].HostName != "logstash-01" {
		testHelper.Error(t, "Should get \"logstash-01\" for \"HostName\", got: %s", servers[0].HostName)
	} else {
		testHelper.Success(t, "Should get \"logstash-01\" for \"HostName\"")
	}

	if servers[0].DomainName != "albuquerque.nm.albuq.kabletown.com" {
		testHelper.Error(t, "Should get \"albuquerque.nm.albuq.kabletown.com\" for \"DomainName\", got: %s", servers[0].DomainName)
	} else {
		testHelper.Success(t, "Should get \"albuquerque.nm.albuq.kabletown.com\" for \"DomainName\"")
	}

	if servers[0].Type != "LOGSTASH" {
		testHelper.Error(t, "Should get \"LOGSTASH\" for \"Type\", got: %s", servers[0].Type)
	} else {
		testHelper.Success(t, "Should get \"LOGSTASH\" for \"Type\"")
	}
}
예제 #16
0
func TestHardware(t *testing.T) {
	resp := fixtures.Hardware()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for Hardware")

	hardware, err := to.Hardware()
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if len(hardware) != 1 {
		testHelper.Error(t, "Should get back \"1\" Hardware, got: %d", len(hardware))
	} else {
		testHelper.Success(t, "Should get back \"1\" Hardware")
	}

	for _, h := range hardware {
		if h.HostName != "odol-atsmid-cen-09" {
			testHelper.Error(t, "Should get back \"odol-atsmid-cen-09\" for \"Hostname\", got: %s", h.HostName)
		} else {
			testHelper.Success(t, "Should get back \"odol-atsmid-cen-09\" for \"Hostname\"")
		}

		if h.Value != "1.00" {
			testHelper.Error(t, "Should get back \"1.00\" for \"Value\", got: %s", h.Value)
		} else {
			testHelper.Success(t, "Should get back \"1.00\" for \"Value\"")
		}

		if h.Description != "BACKPLANE FIRMWARE" {
			testHelper.Error(t, "Should get back \"BACKPLANE FIRMWARE\" for \"Description\", got: %s", h.Description)
		} else {
			testHelper.Success(t, "Should get back \"BACKPLANE FIRMWARE\" for \"Description\"")
		}
	}
}
예제 #17
0
func TestParameters(t *testing.T) {
	resp := fixtures.Parameters()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for Parameters")

	parameters, err := to.Parameters("test")
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if len(parameters) != 1 {
		testHelper.Error(t, "Should get back \"1\" Parameter, got: %d", len(parameters))
	} else {
		testHelper.Success(t, "Should get back \"1\" Parameter")
	}

	for _, param := range parameters {
		if param.Name != "location" {
			testHelper.Error(t, "Should get back \"location\" for \"Name\", got: %s", param.Name)
		} else {
			testHelper.Success(t, "Should get back \"location\" for \"Name\"")
		}

		if param.Value != "/foo/trafficserver/" {
			testHelper.Error(t, "Should get back \"/foo/trafficserver/\" for \"Value\", got: %s", param.Value)
		} else {
			testHelper.Success(t, "Should get back \"/foo/trafficserver/\" for \"Value\"")
		}

		if param.ConfigFile != "parent.config" {
			testHelper.Error(t, "Should get back \"parent.config\" for \"ConfigFile\", got: %s", param.ConfigFile)
		} else {
			testHelper.Success(t, "Should get back \"parent.config\" for \"ConfigFile\"")
		}
	}
}
예제 #18
0
func TestServer(t *testing.T) {
	resp := fixtures.Servers()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for Servers")

	servers, err := to.Servers()
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if len(servers) != 3 {
		testHelper.Error(t, "Should get back \"3\" Server, got: %d", len(servers))
	} else {
		testHelper.Success(t, "Should get back \"3\" Server")
	}

	if servers[0].HostName != "edge-alb-01" {
		testHelper.Error(t, "Should get \"edge-alb-01\" for \"HostName\", got: %s", servers[0].HostName)
	} else {
		testHelper.Success(t, "Should get \"edge-alb-01\" for \"HostName\"")
	}

	if servers[0].DomainName != "albuquerque.nm.albuq.kabletown.com" {
		testHelper.Error(t, "Should get \"albuquerque.nm.albuq.kabletown.com\" for \"DomainName\", got: %s", servers[0].DomainName)
	} else {
		testHelper.Success(t, "Should get \"albuquerque.nm.albuq.kabletown.com\" for \"DomainName\"")
	}

	if servers[0].Type != "EDGE" {
		testHelper.Error(t, "Should get \"EDGE\" for \"Type\", got: %s", servers[0].Type)
	} else {
		testHelper.Success(t, "Should get \"EDGE\" for \"Type\"")
	}
}
예제 #19
0
func TestServersUnauthorized(t *testing.T) {
	server := testHelper.InvalidHTTPServer(http.StatusUnauthorized)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a failed Traffic Ops request for Servers")

	_, err := to.Servers()
	if err == nil {
		testHelper.Error(t, "Should not be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should not be able to make a request to Traffic Ops")
	}
}
예제 #20
0
func TestServerFQDNUnauthorized(t *testing.T) {
	server := testHelper.InvalidHTTPServer(http.StatusUnauthorized)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	shortName := "edge-alb-01"
	testHelper.Context(t, "Given the need to test a failed Traffic Ops request for the FQDN of Server: \"%s\"", shortName)

	_, err := to.ServersFqdn(shortName)
	if err == nil {
		testHelper.Error(t, "Should not be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should not be able to make a request to Traffic Ops")
	}
}
예제 #21
0
func TestServerShortNameUnauthorized(t *testing.T) {
	server := testHelper.InvalidHTTPServer(http.StatusUnauthorized)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	pattern := "edge"
	testHelper.Context(t, "Given the need to test a failed Traffic Ops request for servers that match Short Name: \"%s\"", pattern)

	_, err := to.ServersShortNameSearch(pattern)
	if err == nil {
		testHelper.Error(t, "Should not be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should not be able to make a request to Traffic Ops")
	}
}
예제 #22
0
func TestProfile(t *testing.T) {
	resp := fixtures.Profiles()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for Profiles")

	profiles, err := to.Profiles()
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if len(profiles) != 1 {
		testHelper.Error(t, "Should get back \"1\" Profile, got: %d", len(profiles))
	} else {
		testHelper.Success(t, "Should get back \"1\" Profile")
	}

	for _, p := range profiles {
		if p.Name != "TR_CDN2" {
			testHelper.Error(t, "Should get back \"TR_CDN2\" for \"Name\", got: %s", p.Name)
		} else {
			testHelper.Success(t, "Should get back \"TR_CDN2\" for \"Name\"")
		}

		if p.Description != "kabletown Content Router" {
			testHelper.Error(t, "Should get back \"kabletown Content Router\" for \"Description\", got: %s", p.Description)
		} else {
			testHelper.Success(t, "Should get back \"kabletown Content Router\" for \"Description\"")
		}
	}
}
예제 #23
0
func TestServerByTypeUnauthorized(t *testing.T) {
	server := testHelper.InvalidHTTPServer(http.StatusUnauthorized)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a failed Traffic Ops request for \"Logstash\" servers")

	params := make(url.Values)
	params.Add("type", "Logstash")

	_, err := to.ServersByType(params)
	if err == nil {
		testHelper.Error(t, "Should not be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should not be able to make a request to Traffic Ops")
	}
}
예제 #24
0
// getServerCachegroups gets the cachegroup of each ATS Edge+Mid Cache server, for the given CDN, from Traffic Ops.
// Returns a map[server]cachegroup.
func getServerCachegroups(to *traffic_ops.Session, cdn string) (map[string]string, error) {
	serverCachegroups := map[string]string{}

	crcData, err := to.CRConfigRaw(cdn)
	if err != nil {
		return nil, err
	}
	type CrConfig struct {
		ContentServers map[string]struct {
			CacheGroup string `json:"cacheGroup"`
		} `json:"contentServers"`
	}
	var crc CrConfig
	if err := json.Unmarshal(crcData, &crc); err != nil {
		return nil, err
	}

	for server, serverData := range crc.ContentServers {
		serverCachegroups[server] = serverData.CacheGroup
	}
	return serverCachegroups, nil
}
func TestDeliveryServiceHealth(t *testing.T) {
	resp := fixtures.DeliveryServiceHealth()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for a DeliveryServiceHealth")

	health, err := to.DeliveryServiceHealth("123")
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if health.TotalOnline != 2 {
		testHelper.Error(t, "Should get back \"2\" for \"TotalOnline\", got: %s", health.TotalOnline)
	} else {
		testHelper.Success(t, "Should get back \"2\" for \"TotalOnline\"")
	}

	if health.TotalOffline != 3 {
		testHelper.Error(t, "Should get back \"3\" for \"TotalOffline\", got: %s", health.TotalOffline)
	} else {
		testHelper.Success(t, "Should get back \"2\" for \"TotalOffline\"")
	}

	if health.CacheGroups[0].Name != "someCacheGroup" {
		testHelper.Error(t, "Should get back \"someCacheGroup\" for \"CacheGroups[0].Name\", got: %s", health.CacheGroups[0].Name)
	} else {
		testHelper.Success(t, "Should get back \"someCacheGroup\" for \"CacheGroups[0].Name\"")
	}
}
func TestDeliveryServiceCapacity(t *testing.T) {
	resp := fixtures.DeliveryServiceCapacity()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for a DeliveryServiceCapacity")

	capacity, err := to.DeliveryServiceCapacity("123")
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	if capacity.AvailablePercent != 90.12345 {
		testHelper.Error(t, "Should get back \"90.12345\" for \"AvailablePercent\", got: %s", capacity.AvailablePercent)
	} else {
		testHelper.Success(t, "Should get back \"90.12345\" for \"AvailablePercent\"")
	}

	if capacity.UnavailablePercent != 90.12345 {
		testHelper.Error(t, "Should get back \"90.12345\" for \"UnavailablePercent\", got: %s", capacity.UnavailablePercent)
	} else {
		testHelper.Success(t, "Should get back \"90.12345\" for \"UnavailablePercent\"")
	}

	if capacity.UtilizedPercent != 90.12345 {
		testHelper.Error(t, "Should get back \"90.12345\" for \"UtilizedPercent\", got: %s", capacity.UtilizedPercent)
	} else {
		testHelper.Success(t, "Should get back \"90.12345\" for \"UtilizedPercent\"")
	}
}
예제 #27
0
//
// Kicks off the pollers and handlers
//
func Start(opsConfigFile string, staticAppData StaticAppData) {
	var toSession *traffic_ops.Session

	fetchSuccessCounter := gmx.NewCounter("fetchSuccess")
	fetchFailCounter := gmx.NewCounter("fetchFail")
	fetchPendingGauge := gmx.NewGauge("fetchPending")

	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}

	sharedClient := http.Client{
		Timeout:   defaultHttpTimeout,
		Transport: tr,
	}

	cacheHealthConfigChannel := make(chan poller.HttpPollerConfig)
	cacheHealthChannel := make(chan cache.Result)
	cacheHealthTick := make(chan uint64)
	cacheHealthPoller := poller.HttpPoller{
		TickChan:      cacheHealthTick,
		ConfigChannel: cacheHealthConfigChannel,
		Config: poller.HttpPollerConfig{
			Interval: defaultCacheHealthPollingInterval,
		},
		Fetcher: fetcher.HttpFetcher{
			Handler: cache.Handler{ResultChannel: cacheHealthChannel},
			Client:  sharedClient,
			Success: fetchSuccessCounter,
			Fail:    fetchFailCounter,
			Pending: fetchPendingGauge,
		},
	}

	cacheStatConfigChannel := make(chan poller.HttpPollerConfig)
	cacheStatChannel := make(chan cache.Result)
	cacheStatPoller := poller.HttpPoller{
		ConfigChannel: cacheStatConfigChannel,
		Config: poller.HttpPollerConfig{
			Interval: defaultCacheStatPollingInterval,
		},
		Fetcher: fetcher.HttpFetcher{
			Handler: cache.Handler{ResultChannel: cacheStatChannel},
			Client:  sharedClient,
			Success: fetchSuccessCounter,
			Fail:    fetchFailCounter,
			Pending: fetchPendingGauge,
		},
	}

	sessionChannel := make(chan *traffic_ops.Session)
	monitorConfigChannel := make(chan traffic_ops.TrafficMonitorConfigMap)
	monitorOpsConfigChannel := make(chan handler.OpsConfig)
	monitorConfigPoller := poller.MonitorConfigPoller{
		Interval:         defaultMonitorConfigPollingInterval,
		SessionChannel:   sessionChannel,
		ConfigChannel:    monitorConfigChannel,
		OpsConfigChannel: monitorOpsConfigChannel,
	}

	opsConfigFileChannel := make(chan interface{})
	opsConfigFilePoller := poller.FilePoller{
		File:          opsConfigFile,
		ResultChannel: opsConfigFileChannel,
	}

	opsConfigChannel := make(chan handler.OpsConfig)
	opsConfigFileHandler := handler.OpsConfigFileHandler{
		ResultChannel:    opsConfigFilePoller.ResultChannel,
		OpsConfigChannel: opsConfigChannel,
	}

	peerConfigChannel := make(chan poller.HttpPollerConfig)
	peerChannel := make(chan peer.Result)
	peerPoller := poller.HttpPoller{
		ConfigChannel: peerConfigChannel,
		Config: poller.HttpPollerConfig{
			Interval: defaultPeerPollingInterval,
		},
		Fetcher: fetcher.HttpFetcher{
			Handler: peer.Handler{ResultChannel: peerChannel},
			Client:  sharedClient,
			Success: fetchSuccessCounter,
			Fail:    fetchFailCounter,
			Pending: fetchPendingGauge,
		},
	}

	go opsConfigFileHandler.Listen()
	go opsConfigFilePoller.Poll()
	go monitorConfigPoller.Poll()
	go cacheHealthPoller.Poll()
	go cacheStatPoller.Poll()
	go peerPoller.Poll()

	dr := make(chan http_server.DataRequest)

	healthHistory := make(map[string][]interface{})
	statHistory := make(map[string][]interface{})

	var opsConfig handler.OpsConfig
	var monitorConfig traffic_ops.TrafficMonitorConfigMap
	localStates := peer.Crstates{Caches: make(map[string]peer.IsAvailable), Deliveryservice: make(map[string]peer.Deliveryservice)}    // this is the local state as discoverer by this traffic_monitor
	peerStates := make(map[string]peer.Crstates)                                                                                       // each peer's last state is saved in this map
	combinedStates := peer.Crstates{Caches: make(map[string]peer.IsAvailable), Deliveryservice: make(map[string]peer.Deliveryservice)} // this is the result of combining the localStates and all the peerStates using the var ??

	deliveryServiceServers := map[string][]string{}
	serverTypes := map[string]string{}

	// TODO put stat data in a struct, for brevity
	lastHealthEndTimes := map[string]time.Time{}
	lastHealthDurations := map[string]time.Duration{}
	fetchCount := uint64(0) // note this is the number of individual caches fetched from, not the number of times all the caches were polled.
	healthIteration := uint64(0)
	errorCount := uint64(0)
	events := []Event{}
	eventIndex := uint64(0)
	for {
		select {
		case req := <-dr:
			defer close(req.C)

			var body []byte
			var err error

			switch req.T {
			case http_server.TR_CONFIG:
				if toSession != nil && opsConfig.CdnName != "" {
					body, err = toSession.CRConfigRaw(opsConfig.CdnName)
				}
			case http_server.TR_STATE_DERIVED:
				body, err = peer.CrStatesMarshall(combinedStates)
			case http_server.TR_STATE_SELF:
				body, err = peer.CrStatesMarshall(localStates)
			case http_server.CACHE_STATS:
				// TODO: add support for ?hc=N query param, stats=, wildcard, individual caches
				// add pp and date to the json:
				/*
					pp: "0=[my-ats-edge-cache-1], hc=[1]",
					date: "Thu Oct 09 20:28:36 UTC 2014"
				*/
				params := req.Parameters
				hc := 1
				if _, exists := params["hc"]; exists {
					v, err := strconv.Atoi(params["hc"][0])
					if err == nil {
						hc = v
					}
				}
				body, err = cache.StatsMarshall(statHistory, hc)
			case http_server.DS_STATS:
				body = []byte("TODO implement")
			case http_server.EVENT_LOG:
				body, err = json.Marshal(JSONEvents{Events: events})
			case http_server.PEER_STATES:
				body = []byte("TODO implement")
			case http_server.STAT_SUMMARY:
				body = []byte("TODO implement")
			case http_server.STATS:
				body, err = getStats(staticAppData, cacheHealthPoller.Config.Interval, lastHealthDurations, fetchCount, healthIteration, errorCount)
				if err != nil {
					// TODO send error to client
					errorCount++
					log.Printf("ERROR getting stats %v\n", err)
					continue
				}
			case http_server.CONFIG_DOC:
				opsConfigCopy := opsConfig
				// if the password is blank, leave it blank, so callers can see it's missing.
				if opsConfigCopy.Password != "" {
					opsConfigCopy.Password = "******"
				}
				body, err = json.Marshal(opsConfigCopy)
			default:
				body = []byte("TODO error message")
			}
			req.C <- body
		case oc := <-opsConfigFileHandler.OpsConfigChannel:
			var err error
			opsConfig = oc

			listenAddress := ":80" // default

			if opsConfig.HttpListener != "" {
				listenAddress = opsConfig.HttpListener
			}

			err = http_server.Run(dr, listenAddress)
			if err != nil {
				errorCount++
				log.Printf("MonitorConfigPoller: error creating HTTP server: %s\n", err)
				continue
			}

			toSession, err = traffic_ops.Login(opsConfig.Url, opsConfig.Username, opsConfig.Password, opsConfig.Insecure)
			if err != nil {
				errorCount++
				log.Printf("MonitorConfigPoller: error instantiating Session with traffic_ops: %s\n", err)
				continue
			}

			deliveryServiceServers, err = getDeliveryServiceServers(toSession, opsConfig.CdnName)
			if err != nil {
				errorCount++
				log.Printf("Error getting delivery service servers from Traffic Ops: %v\n", err)
				continue
			}

			serverTypes, err = getServerTypes(toSession, opsConfig.CdnName)
			if err != nil {
				errorCount++
				log.Printf("Error getting server types from Traffic Ops: %v\n", err)
				continue
			}

			// This must be in a goroutine, because the monitorConfigPoller tick sends to a channel this select listens for. Thus, if we block on sends to the monitorConfigPoller, we have a livelock race condition.
			go func() {
				monitorConfigPoller.OpsConfigChannel <- opsConfig // this is needed for cdnName
				monitorConfigPoller.SessionChannel <- toSession
			}()
		case monitorConfig = <-monitorConfigPoller.ConfigChannel:
			healthUrls := map[string]string{}
			statUrls := map[string]string{}
			peerUrls := map[string]string{}
			caches := map[string]string{}

			for _, srv := range monitorConfig.TrafficServer {
				caches[srv.HostName] = srv.Status

				if srv.Status == "ONLINE" {
					localStates.Caches[srv.HostName] = peer.IsAvailable{IsAvailable: true}
					continue
				}
				if srv.Status == "OFFLINE" {
					localStates.Caches[srv.HostName] = peer.IsAvailable{IsAvailable: false}
					continue
				}
				// seed states with available = false until our polling cycle picks up a result
				if _, exists := localStates.Caches[srv.HostName]; !exists {
					localStates.Caches[srv.HostName] = peer.IsAvailable{IsAvailable: false}
				}

				url := monitorConfig.Profile[srv.Profile].Parameters.HealthPollingURL
				r := strings.NewReplacer(
					"${hostname}", srv.FQDN,
					"${interface_name}", srv.InterfaceName,
					"application=system", "application=plugin.remap",
					"application=", "application=plugin.remap",
				)
				url = r.Replace(url)
				healthUrls[srv.HostName] = url
				r = strings.NewReplacer("application=plugin.remap", "application=")
				url = r.Replace(url)
				statUrls[srv.HostName] = url
			}

			for _, srv := range monitorConfig.TrafficMonitor {
				if srv.Status != "ONLINE" {
					continue
				}
				// TODO: the URL should be config driven. -jse
				url := fmt.Sprintf("http://%s:%d/publish/CrStates?raw", srv.IP, srv.Port)
				peerUrls[srv.HostName] = url
			}

			cacheStatPoller.ConfigChannel <- poller.HttpPollerConfig{Urls: statUrls, Interval: defaultCacheStatPollingInterval}
			cacheHealthPoller.ConfigChannel <- poller.HttpPollerConfig{Urls: healthUrls, Interval: defaultCacheHealthPollingInterval}
			peerPoller.ConfigChannel <- poller.HttpPollerConfig{Urls: peerUrls, Interval: defaultPeerPollingInterval}

			for k := range localStates.Caches {
				_, exists := monitorConfig.TrafficServer[k]

				if !exists {
					fmt.Printf("Warning: removing %s from localStates", k)
					delete(localStates.Caches, k)
				}
			}

			addStateDeliveryServices(monitorConfig, localStates.Deliveryservice)
		case i := <-cacheHealthTick:
			healthIteration = i
		case healthResult := <-cacheHealthChannel:
			fetchCount++
			var prevResult cache.Result
			if len(healthHistory[healthResult.Id]) != 0 {
				prevResult = healthHistory[healthResult.Id][len(healthHistory[healthResult.Id])-1].(cache.Result)
			}
			health.GetVitals(&healthResult, &prevResult, &monitorConfig)
			healthHistory[healthResult.Id] = pruneHistory(append(healthHistory[healthResult.Id], healthResult), defaultMaxHistory)
			isAvailable, whyAvailable := health.EvalCache(healthResult, &monitorConfig)
			if localStates.Caches[healthResult.Id].IsAvailable != isAvailable {
				fmt.Println("Changing state for", healthResult.Id, " was:", prevResult.Available, " is now:", isAvailable, " because:", whyAvailable, " errors:", healthResult.Errors)
				e := Event{
					Index:       eventIndex,
					Time:        time.Now().Unix(),
					Description: whyAvailable,
					Name:        healthResult.Id,
					Hostname:    healthResult.Id,
					Type:        serverTypes[healthResult.Id],
					Available:   isAvailable,
				}
				events = append([]Event{e}, events...)
				if len(events) > maxEvents {
					events = events[:maxEvents-1]
				}
				eventIndex++
			}
			localStates.Caches[healthResult.Id] = peer.IsAvailable{IsAvailable: isAvailable}
			calculateDeliveryServiceState(deliveryServiceServers, localStates.Caches, localStates.Deliveryservice)

			if lastHealthStart, ok := lastHealthEndTimes[healthResult.Id]; ok {
				lastHealthDurations[healthResult.Id] = time.Since(lastHealthStart)
			}
			lastHealthEndTimes[healthResult.Id] = time.Now()

			// if _, ok := queryIntervalStart[pollI]; !ok {
			// 	log.Printf("ERROR poll start index not found")
			// 	continue
			// }
			// lastQueryIntervalTime = time.Since(queryIntervalStart[pollI])
		case stats := <-cacheStatChannel:
			statHistory[stats.Id] = pruneHistory(append(statHistory[stats.Id], stats), defaultMaxHistory)
		case crStatesResult := <-peerChannel:
			peerStates[crStatesResult.Id] = crStatesResult.PeerStats
			combinedStates = combineCrStates(peerStates, localStates)
		}
	}
}
func TestTMConfig(t *testing.T) {
	resp := fixtures.TrafficMonitorConfig()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TM Config")

	tm, err := to.TrafficMonitorConfigMap("test-cdn")
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TM Config - Traffic Server")

	ts := tm.TrafficServer
	if len(ts) != 2 {
		testHelper.Error(t, "Should get back \"2\" Traffic Servers, got: %d", len(ts))
	} else {
		testHelper.Success(t, "Should get back \"2\" Traffic Servers")
	}

	hashID := "tr-chi-05"
	if val, ok := ts[hashID]; ok {
		testHelper.Success(t, "Should get back map entry for \"%s\"", hashID)

		if val.IP != "10.10.10.10" {
			testHelper.Error(t, "Should get back \"10.10.10.10\" for \"IP\", got: %s", val.IP)
		} else {
			testHelper.Success(t, "Should get back \"10.10.10.10\" for \"IP\"")
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", hashID)
	}

	hashID = "edge-test-01"
	if val, ok := ts[hashID]; ok {
		testHelper.Success(t, "Should get back map entry for for \"%s\"", hashID)

		if val.Type != "EDGE" {
			testHelper.Error(t, "Should get back \"EDGE\" for \"%s\" \"Type\", got: %s", hashID, ts[hashID].Type)
		} else {
			testHelper.Success(t, "Should get back \"EDGE\" for \"%s\" \"Type\"", hashID)
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", hashID)
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TR Config - Traffic Monitor")

	m := tm.TrafficMonitor
	if len(m) != 1 {
		testHelper.Error(t, "Should get back \"1\" Traffic Servers, got: %d", len(m))
	} else {
		testHelper.Success(t, "Should get back \"1\" Traffic Servers")
	}

	hostName := "traffic-monitor-01"
	if val, ok := m[hostName]; ok {
		testHelper.Success(t, "Should get back map entry for \"%s\"", hostName)

		if val.Status != "ONLINE" {
			testHelper.Error(t, "Should get back \"ONLINE\" for \"Status\", got: %s", val.Status)
		} else {
			testHelper.Success(t, "Should get back \"ONLINE\" for \"Status\"")
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", hostName)
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TM Config - CacheGroups")

	c := tm.CacheGroup
	if len(c) != 2 {
		testHelper.Error(t, "Should get back \"2\" Traffic Servers, got: %d", len(c))
	} else {
		testHelper.Success(t, "Should get back \"2\" Traffic Servers")
	}

	name := "philadelphia"
	if val, ok := c[name]; ok {
		testHelper.Success(t, "Should get back map entry for \"%s\"", name)

		if val.Coordinates.Latitude != 55 {
			testHelper.Error(t, "Should get back \"55\" for \"Coordinates.Latitude\", got: %s", val.Coordinates.Latitude)
		} else {
			testHelper.Success(t, "Should get back \"55\" for \"Coordinates.Latitude\"")
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", name)
	}

	name = "tr-chicago"
	if val, ok := c[name]; ok {
		testHelper.Success(t, "Should get back map entry for \"%s\"", name)

		if val.Coordinates.Longitude != 9 {
			testHelper.Error(t, "Should get back \"9\" for \"Coordinates.Longitude\", got: %s", val.Coordinates.Longitude)
		} else {
			testHelper.Success(t, "Should get back \"9\" for \"Coordinates.Longitude\"")
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", name)
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TM Config - Delivery Services")

	ds := tm.DeliveryService
	if len(ds) != 1 {
		testHelper.Error(t, "Should get back \"1\" TM Delivery Service, got: %d", len(ds))
	} else {
		testHelper.Success(t, "Should get back \"1\" TM Delivery Service")
	}

	xmlID := "ds-05"
	if val, ok := ds[xmlID]; ok {
		testHelper.Success(t, "Should get back map entry for \"%s\"", xmlID)

		if val.Status != "REPORTED" {
			testHelper.Error(t, "Should get back \"REPORTED\" for \"Status\", got: %s", val.Status)
		} else {
			testHelper.Success(t, "Should get back \"REPORTED\" for \"Status\"")
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", xmlID)
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TM Config - TM Profiles")

	p := tm.Profile
	if len(p) != 1 {
		testHelper.Error(t, "Should get back \"1\" TM Profie, got: %d", len(p))
	} else {
		testHelper.Success(t, "Should get back \"1\" TM Profile")
	}

	name = "tm-123"
	if val, ok := p[name]; ok {
		testHelper.Success(t, "Should get back map entry for \"%s\"", name)

		if val.Parameters.HealthConnectionTimeout != 2000 {
			testHelper.Error(t, "Should get back \"2000\" for \"Parameters.HealthConnectionTimeout\", got: %v", val.Parameters.HealthConnectionTimeout)
		} else {
			testHelper.Success(t, "Should get back \"2000\" for \"Parameters.HealthConnectionTimeout\"")
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", name)
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TM Config - Config")

	conf := tm.Config
	if _, ok := conf["peers.polling.interval"]; ok {
		if conf["peers.polling.interval"] != float64(1000) {
			testHelper.Error(t, "Should get back \"1000\" for map entry for \"peers.polling.interval\", got: \"%v\"", conf["peers.polling.interval"])
		} else {
			testHelper.Success(t, "Should get back \"1000\" for map entry for \"peers.polling.interval\"")
		}
	}
}
func TestTRConfig(t *testing.T) {
	resp := fixtures.TrafficRouterConfig()
	server := testHelper.ValidHTTPServer(resp)
	defer server.Close()

	var httpClient http.Client
	to := client.Session{
		URL:       server.URL,
		UserAgent: &httpClient,
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TR Config")

	tr, err := to.TrafficRouterConfigMap("title-vi")
	if err != nil {
		testHelper.Error(t, "Should be able to make a request to Traffic Ops")
	} else {
		testHelper.Success(t, "Should be able to make a request to Traffic Ops")
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TR Config - Traffic Server")

	ts := tr.TrafficServer
	if len(ts) != 2 {
		testHelper.Error(t, "Should get back \"2\" Traffic Servers, got: %d", len(ts))
	} else {
		testHelper.Success(t, "Should get back \"2\" Traffic Servers")
	}

	hashID := "tr-chi-05"
	if val, ok := ts[hashID]; ok {
		testHelper.Success(t, "Should get back map entry for \"%s\"", hashID)

		if val.IP != "10.10.10.10" {
			testHelper.Error(t, "Should get back \"10.10.10.10\" for \"IP\", got: %s", val.IP)
		} else {
			testHelper.Success(t, "Should get back \"10.10.10.10\" for \"IP\"")
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", hashID)
	}

	hashID = "edge-test-01"
	if val, ok := ts[hashID]; ok {
		testHelper.Success(t, "Should get back map entry for for \"%s\"", hashID)

		if val.Type != "EDGE" {
			testHelper.Error(t, "Should get back \"EDGE\" for \"%s\" \"Type\", got: %s", hashID, ts[hashID].Type)
		} else {
			testHelper.Success(t, "Should get back \"EDGE\" for \"%s\" \"Type\"", hashID)
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", hashID)
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TR Config - Traffic Monitor")

	tm := tr.TrafficMonitor
	if len(tm) != 1 {
		testHelper.Error(t, "Should get back \"1\" Traffic Servers, got: %d", len(tm))
	} else {
		testHelper.Success(t, "Should get back \"1\" Traffic Servers")
	}

	hostname := "traffic-monitor-01"
	if val, ok := tm[hostname]; ok {
		testHelper.Success(t, "Should get back map entry for \"%s\"", hostname)

		if val.Profile != "tr-123" {
			testHelper.Error(t, "Should get back \"tr-123\" for \"Profile\", got: %s", val.Profile)
		} else {
			testHelper.Success(t, "Should get back \"tr-123\" for \"Profile\"")
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", hostname)
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TR Config - Traffic Router")

	r := tr.TrafficRouter
	if len(r) != 1 {
		testHelper.Error(t, "Should get back \"1\" Traffic Servers, got: %d", len(r))
	} else {
		testHelper.Success(t, "Should get back \"1\" Traffic Servers")
	}

	fqdn := "*****@*****.**"
	if val, ok := r[fqdn]; ok {
		testHelper.Success(t, "Should get back map entry for \"%s\"", fqdn)

		if val.Location != "tr-chicago" {
			testHelper.Error(t, "Should get back \"tr-chicago\" for \"Location\", got: %s", val.Location)
		} else {
			testHelper.Success(t, "Should get back \"tr-chicago\" for \"Location\"")
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", fqdn)
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TR Config - CacheGroups")

	c := tr.CacheGroup
	if len(c) != 2 {
		testHelper.Error(t, "Should get back \"2\" Traffic Servers, got: %d", len(c))
	} else {
		testHelper.Success(t, "Should get back \"2\" Traffic Servers")
	}

	name := "philadelphia"
	if val, ok := c[name]; ok {
		testHelper.Success(t, "Should get back map entry for \"%s\"", name)

		if val.Coordinates.Latitude != 99 {
			testHelper.Error(t, "Should get back \"99\" for \"Coordinates.Latitude\", got: %v", val.Coordinates.Latitude)
		} else {
			testHelper.Success(t, "Should get back \"99\" for \"Coordinates.Latitude\"")
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", name)
	}

	name = "tr-chicago"
	if val, ok := c[name]; ok {
		testHelper.Success(t, "Should get back map entry for \"%s\"", name)

		if val.Coordinates.Longitude != 9 {
			testHelper.Error(t, "Should get back \"9\" for \"Coordinates.Longitude\", got: %v", val.Coordinates.Longitude)
		} else {
			testHelper.Success(t, "Should get back \"9\" for \"Coordinates.Longitude\"")
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", name)
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TR Config - Delivery Services")

	ds := tr.DeliveryService
	if len(ds) != 1 {
		testHelper.Error(t, "Should get back \"1\" TR Delivery Service, got: %d", len(ds))
	} else {
		testHelper.Success(t, "Should get back \"1\" TR Delivery Service")
	}

	xmlID := "ds-06"
	if val, ok := ds[xmlID]; ok {
		testHelper.Success(t, "Should get back map entry for \"%s\"", xmlID)

		if val.TTL != 3600 {
			testHelper.Error(t, "Should get back \"3600\" for \"TTL\", got: %d", val.TTL)
		} else {
			testHelper.Success(t, "Should get back \"3600\" for \"TTL\"")
		}

	} else {
		testHelper.Error(t, "Should get back map entry for \"%s\"", xmlID)
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TR Config - Config")

	conf := tr.Config
	if _, ok := conf["peers.polling.interval"]; ok {
		if conf["peers.polling.interval"] != float64(1000) {
			testHelper.Error(t, "Should get back \"1000\" for map entry for \"peers.polling.interval\", got: \"%v\"", conf["peers.polling.interval"])
		} else {
			testHelper.Success(t, "Should get back \"1000\" for map entry for \"peers.polling.interval\"")
		}
	}

	testHelper.Context(t, "Given the need to test a successful Traffic Ops request for TR Config - Stats")

	stats := tr.Stat
	if _, ok := stats["cdnName"]; ok {
		if stats["cdnName"] != "test-cdn" {
			testHelper.Error(t, "Should get back \"test-cdn\" for map entry for \"cdnName\", got: \"%s\"", stats["cdnName"])
		} else {
			testHelper.Success(t, "Should get back \"test-cdn\" for map entry for \"cdnName\"")
		}
	}
}