github.com/infobloxopen/infoblox-go-client@v1.1.1/objects_test.go (about)

     1  package ibclient
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("Objects", func() {
    11  
    12  	Context("Grid object", func() {
    13  
    14  		tesNtpserver := NTPserver{
    15  			Address:              "16.4.1.2",
    16  			Burst:                true,
    17  			EnableAuthentication: true,
    18  			IBurst:               true,
    19  			Preffered:            true,
    20  		}
    21  		grid := Grid{Name: "test", NTPSetting: &NTPSetting{EnableNTP: true,
    22  			NTPAcl:     nil,
    23  			NTPKeys:    nil,
    24  			NTPKod:     false,
    25  			NTPServers: []NTPserver{tesNtpserver},
    26  		},
    27  		}
    28  		gridJSON := `{
    29  			"name": "test",
    30  			"ntp_setting": {
    31  				"enable_ntp": true,
    32  				"ntp_servers": [{
    33  					"address": "16.4.1.2",
    34  					"burst": true,
    35  					"enable_authentication": true,
    36  					"iburst": true,
    37  					"preffered": true
    38  					}]
    39  				}
    40  				}`
    41  
    42  		Context("Marshalling", func() {
    43  			Context("expected JSON is returned", func() {
    44  				js, err := json.Marshal(grid)
    45  
    46  				It("should not error", func() {
    47  					Expect(err).NotTo(HaveOccurred())
    48  				})
    49  
    50  				It("should match json expected", func() {
    51  					Expect(js).To(MatchJSON(gridJSON))
    52  				})
    53  			})
    54  		})
    55  
    56  		Context("Unmarshalling", func() {
    57  			Context("expected object is returned", func() {
    58  				var actualGrid Grid
    59  				err := json.Unmarshal([]byte(gridJSON), &actualGrid)
    60  
    61  				It("should not error", func() {
    62  					Expect(err).NotTo(HaveOccurred())
    63  				})
    64  
    65  				It("should match object expected", func() {
    66  					Expect(actualGrid).To(Equal(grid))
    67  				})
    68  			})
    69  		})
    70  
    71  	})
    72  
    73  	Context("EA Object", func() {
    74  
    75  		ea := EA{
    76  			"Cloud API Owned":   Bool(true),
    77  			"Tenant Name":       "Engineering01",
    78  			"Maximum Wait Time": 120,
    79  			"DNS Support":       Bool(false),
    80  			"Routers":           []string{"10.1.2.234", "10.1.2.235"},
    81  		}
    82  		eaJSON := `{"Cloud API Owned":{"value":"True"},` +
    83  			`"Tenant Name":{"value":"Engineering01"},` +
    84  			`"Maximum Wait Time":{"value":120},` +
    85  			`"DNS Support":{"value":"False"},` +
    86  			`"Routers":{"value":["10.1.2.234", "10.1.2.235"]}}`
    87  
    88  		Context("Marshalling", func() {
    89  			Context("expected JSON is returned", func() {
    90  				js, err := json.Marshal(ea)
    91  
    92  				It("should not error", func() {
    93  					Expect(err).NotTo(HaveOccurred())
    94  				})
    95  
    96  				It("should match json expected", func() {
    97  					Expect(js).To(MatchJSON(eaJSON))
    98  				})
    99  			})
   100  		})
   101  
   102  		Context("Unmarshalling", func() {
   103  			Context("expected object is returned", func() {
   104  				var actualEA EA
   105  				err := json.Unmarshal([]byte(eaJSON), &actualEA)
   106  
   107  				It("should not error", func() {
   108  					Expect(err).NotTo(HaveOccurred())
   109  				})
   110  
   111  				It("should match object expected", func() {
   112  					Expect(actualEA).To(Equal(ea))
   113  				})
   114  			})
   115  		})
   116  
   117  	})
   118  
   119  	Context("EA Search Object", func() {
   120  		eas := EASearch{
   121  			"Network Name": "Shared-Net",
   122  			"Network View": "Global",
   123  		}
   124  		expectedJSON := `{"*Network Name" :"Shared-Net",` +
   125  			`"*Network View" :"Global"}`
   126  
   127  		Context("Marshalling", func() {
   128  			Context("expected JSON is returned", func() {
   129  				js, err := json.Marshal(eas)
   130  
   131  				It("should not error", func() {
   132  					Expect(err).NotTo(HaveOccurred())
   133  				})
   134  
   135  				It("should match json expected", func() {
   136  					Expect(js).To(MatchJSON(expectedJSON))
   137  				})
   138  			})
   139  		})
   140  	})
   141  
   142  	Context("EADefListValue Object", func() {
   143  		var eadListVal EADefListValue = "Host Record"
   144  
   145  		eadListValJSON := `{"value": "Host Record"}`
   146  
   147  		Context("Marshalling", func() {
   148  			Context("expected JSON is returned", func() {
   149  				js, err := json.Marshal(eadListVal)
   150  
   151  				It("should not error", func() {
   152  					Expect(err).NotTo(HaveOccurred())
   153  				})
   154  
   155  				It("should match json expected", func() {
   156  					Expect(js).To(MatchJSON(eadListValJSON))
   157  				})
   158  			})
   159  		})
   160  
   161  		Context("Unmarshalling", func() {
   162  			Context("expected object is returned", func() {
   163  				var actualEadListVal EADefListValue
   164  				err := json.Unmarshal([]byte(eadListValJSON), &actualEadListVal)
   165  
   166  				It("should not error", func() {
   167  					Expect(err).NotTo(HaveOccurred())
   168  				})
   169  
   170  				It("should match object expected", func() {
   171  					Expect(actualEadListVal).To(Equal(eadListVal))
   172  				})
   173  			})
   174  		})
   175  
   176  	})
   177  
   178  	Context("Instantiation of", func() {
   179  		Context("NetworkView object", func() {
   180  			name := "myview"
   181  			nv := NewNetworkView(NetworkView{Name: name})
   182  
   183  			It("should set fields correctly", func() {
   184  				Expect(nv.Name).To(Equal(name))
   185  			})
   186  
   187  			It("should set base fields correctly", func() {
   188  				Expect(nv.ObjectType()).To(Equal("networkview"))
   189  				Expect(nv.ReturnFields()).To(ConsistOf("extattrs", "name"))
   190  			})
   191  		})
   192  
   193  		Context("Network object", func() {
   194  			cidr := "123.0.0.0/24"
   195  			netviewName := "localview"
   196  			nw := NewNetwork(Network{Cidr: cidr, NetviewName: netviewName})
   197  			searchEAs := EASearch{"Network Name": "shared-net"}
   198  			nw.eaSearch = searchEAs
   199  
   200  			It("should set fields correctly", func() {
   201  				Expect(nw.Cidr).To(Equal(cidr))
   202  				Expect(nw.NetviewName).To(Equal(netviewName))
   203  			})
   204  
   205  			It("should set base fields correctly", func() {
   206  				Expect(nw.ObjectType()).To(Equal("network"))
   207  				Expect(nw.ReturnFields()).To(ConsistOf("extattrs", "network", "network_view"))
   208  				Expect(nw.EaSearch()).To(Equal(searchEAs))
   209  			})
   210  		})
   211  
   212  		Context("NetworkContainer object", func() {
   213  			cidr := "74.0.8.0/24"
   214  			netviewName := "globalview"
   215  			nwc := NewNetworkContainer(NetworkContainer{Cidr: cidr, NetviewName: netviewName})
   216  
   217  			It("should set fields correctly", func() {
   218  				Expect(nwc.Cidr).To(Equal(cidr))
   219  				Expect(nwc.NetviewName).To(Equal(netviewName))
   220  			})
   221  
   222  			It("should set base fields correctly", func() {
   223  				Expect(nwc.ObjectType()).To(Equal("networkcontainer"))
   224  				Expect(nwc.ReturnFields()).To(ConsistOf("extattrs", "network", "network_view"))
   225  			})
   226  		})
   227  
   228  		Context("FixedAddress object", func() {
   229  			netviewName := "globalview"
   230  			cidr := "25.0.7.0/24"
   231  			ipAddress := "25.0.7.59/24"
   232  			mac := "11:22:33:44:55:66"
   233  			matchClient := "MAC_ADDRESS"
   234  			fixedAddr := NewFixedAddress(FixedAddress{
   235  				NetviewName: netviewName,
   236  				Cidr:        cidr,
   237  				IPAddress:   ipAddress,
   238  				Mac:         mac,
   239  				MatchClient: matchClient})
   240  
   241  			It("should set fields correctly", func() {
   242  				Expect(fixedAddr.NetviewName).To(Equal(netviewName))
   243  				Expect(fixedAddr.Cidr).To(Equal(cidr))
   244  				Expect(fixedAddr.IPAddress).To(Equal(ipAddress))
   245  				Expect(fixedAddr.Mac).To(Equal(mac))
   246  				Expect(fixedAddr.MatchClient).To(Equal(matchClient))
   247  			})
   248  
   249  			It("should set base fields correctly", func() {
   250  				Expect(fixedAddr.ObjectType()).To(Equal("fixedaddress"))
   251  				Expect(fixedAddr.ReturnFields()).To(ConsistOf("extattrs", "ipv4addr", "mac", "name", "network", "network_view"))
   252  			})
   253  		})
   254  
   255  		Context("EADefinition object", func() {
   256  			comment := "Test Extensible Attribute"
   257  			flags := "CGV"
   258  			listValues := []EADefListValue{"True", "False"}
   259  			name := "Test EA"
   260  			eaType := "string"
   261  			allowedTypes := []string{"arecord", "aaarecord", "ptrrecord"}
   262  			eaDef := NewEADefinition(EADefinition{
   263  				Name:               name,
   264  				Comment:            comment,
   265  				Flags:              flags,
   266  				ListValues:         listValues,
   267  				Type:               eaType,
   268  				AllowedObjectTypes: allowedTypes})
   269  
   270  			It("should set fields correctly", func() {
   271  				Expect(eaDef.Comment).To(Equal(comment))
   272  				Expect(eaDef.Flags).To(Equal(flags))
   273  				Expect(eaDef.ListValues).To(ConsistOf(listValues))
   274  				Expect(eaDef.Name).To(Equal(name))
   275  				Expect(eaDef.Type).To(Equal(eaType))
   276  				Expect(eaDef.AllowedObjectTypes).To(ConsistOf(allowedTypes))
   277  			})
   278  
   279  			It("should set base fields correctly", func() {
   280  				Expect(eaDef.ObjectType()).To(Equal("extensibleattributedef"))
   281  				Expect(eaDef.ReturnFields()).To(ConsistOf("allowed_object_types", "comment", "flags", "list_values", "name", "type"))
   282  			})
   283  		})
   284  
   285  		Context("UserProfile object", func() {
   286  			userprofile := NewUserProfile(UserProfile{})
   287  
   288  			It("should set base fields correctly", func() {
   289  				Expect(userprofile.ObjectType()).To(Equal("userprofile"))
   290  				Expect(userprofile.ReturnFields()).To(ConsistOf("name"))
   291  			})
   292  		})
   293  
   294  		Context("RecordA object", func() {
   295  			ipv4addr := "1.1.1.1"
   296  			name := "bind_a.domain.com"
   297  			view := "default"
   298  			zone := "domain.com"
   299  
   300  			ra := NewRecordA(RecordA{
   301  				Ipv4Addr: ipv4addr,
   302  				Name:     name,
   303  				View:     view,
   304  				Zone:     zone})
   305  
   306  			It("should set fields correctly", func() {
   307  				Expect(ra.Ipv4Addr).To(Equal(ipv4addr))
   308  				Expect(ra.Name).To(Equal(name))
   309  				Expect(ra.View).To(Equal(view))
   310  				Expect(ra.Zone).To(Equal(zone))
   311  			})
   312  
   313  			It("should set base fields correctly", func() {
   314  				Expect(ra.ObjectType()).To(Equal("record:a"))
   315  				Expect(ra.ReturnFields()).To(ConsistOf("extattrs", "ipv4addr", "name", "view", "zone"))
   316  			})
   317  		})
   318  
   319  		Context("RecordPtr object", func() {
   320  			ipv4addr := "1.1.1.1"
   321  			ptrdname := "bind_a.domain.com"
   322  			view := "default"
   323  			zone := "domain.com"
   324  
   325  			rptr := NewRecordPTR(RecordPTR{
   326  				Ipv4Addr: ipv4addr,
   327  				PtrdName: ptrdname,
   328  				View:     view,
   329  				Zone:     zone})
   330  
   331  			It("should set fields correctly", func() {
   332  				Expect(rptr.Ipv4Addr).To(Equal(ipv4addr))
   333  				Expect(rptr.PtrdName).To(Equal(ptrdname))
   334  				Expect(rptr.View).To(Equal(view))
   335  				Expect(rptr.Zone).To(Equal(zone))
   336  			})
   337  
   338  			It("should set base fields correctly", func() {
   339  				Expect(rptr.ObjectType()).To(Equal("record:ptr"))
   340  				Expect(rptr.ReturnFields()).To(ConsistOf("extattrs", "ipv4addr", "ptrdname", "view", "zone"))
   341  			})
   342  		})
   343  
   344  		Context("RecordCNAME object", func() {
   345  			canonical := "cname.domain.com"
   346  			name := "bind_cname.domain.com"
   347  			view := "default"
   348  			zone := "domain.com"
   349  
   350  			rc := NewRecordCNAME(RecordCNAME{
   351  				Canonical: canonical,
   352  				Name:      name,
   353  				View:      view,
   354  				Zone:      zone})
   355  
   356  			It("should set fields correctly", func() {
   357  				Expect(rc.Canonical).To(Equal(canonical))
   358  				Expect(rc.Name).To(Equal(name))
   359  				Expect(rc.View).To(Equal(view))
   360  				Expect(rc.Zone).To(Equal(zone))
   361  			})
   362  
   363  			It("should set base fields correctly", func() {
   364  				Expect(rc.ObjectType()).To(Equal("record:cname"))
   365  				Expect(rc.ReturnFields()).To(ConsistOf("extattrs", "canonical", "name", "view", "zone", "ttl", "use_ttl"))
   366  			})
   367  		})
   368  
   369  		Context("RecordHostIpv4Addr object", func() {
   370  			netviewName := "globalview"
   371  			cidr := "25.0.7.0/24"
   372  			ipAddress := "25.0.7.59/24"
   373  			mac := "11:22:33:44:55:66"
   374  			hostAddr := NewHostRecordIpv4Addr(HostRecordIpv4Addr{
   375  				View:     netviewName,
   376  				Cidr:     cidr,
   377  				Ipv4Addr: ipAddress,
   378  				Mac:      mac})
   379  
   380  			It("should set fields correctly", func() {
   381  				Expect(hostAddr.View).To(Equal(netviewName))
   382  				Expect(hostAddr.Cidr).To(Equal(cidr))
   383  				Expect(hostAddr.Ipv4Addr).To(Equal(ipAddress))
   384  				Expect(hostAddr.Mac).To(Equal(mac))
   385  			})
   386  
   387  			It("should set base fields correctly", func() {
   388  				Expect(hostAddr.ObjectType()).To(Equal("record:host_ipv4addr"))
   389  				//Expect(hostAddr.ReturnFields()).To(ConsistOf("configure_for_dhcp", "host", "ipv4addr", "mac"))
   390  			})
   391  		})
   392  
   393  		Context("RecordHostIpv4Addr macaddress empty", func() {
   394  			netviewName := "globalview"
   395  			cidr := "25.0.7.0/24"
   396  			ipAddress := "25.0.7.59/24"
   397  			hostAddr := NewHostRecordIpv4Addr(HostRecordIpv4Addr{
   398  				View:     netviewName,
   399  				Cidr:     cidr,
   400  				Ipv4Addr: ipAddress})
   401  
   402  			It("should set fields correctly", func() {
   403  				Expect(hostAddr.View).To(Equal(netviewName))
   404  				Expect(hostAddr.Cidr).To(Equal(cidr))
   405  				Expect(hostAddr.Ipv4Addr).To(Equal(ipAddress))
   406  			})
   407  
   408  			It("should set base fields correctly", func() {
   409  				Expect(hostAddr.ObjectType()).To(Equal("record:host_ipv4addr"))
   410  				//Expect(hostAddr.ReturnFields()).To(ConsistOf("configure_for_dhcp", "host", "ipv4addr", "mac"))
   411  			})
   412  		})
   413  		Context("RecordHost object", func() {
   414  			ipv4addrs := []HostRecordIpv4Addr{{Ipv4Addr: "1.1.1.1"}, {Ipv4Addr: "2.2.2.2"}}
   415  			name := "bind_host.domain.com"
   416  			view := "default"
   417  			zone := "domain.com"
   418  
   419  			rh := NewHostRecord(HostRecord{
   420  				Ipv4Addrs: ipv4addrs,
   421  				Name:      name,
   422  				View:      view,
   423  				Zone:      zone})
   424  
   425  			It("should set fields correctly", func() {
   426  				Expect(rh.Ipv4Addrs).To(Equal(ipv4addrs))
   427  				Expect(rh.Name).To(Equal(name))
   428  				Expect(rh.View).To(Equal(view))
   429  				Expect(rh.Zone).To(Equal(zone))
   430  			})
   431  
   432  			It("should set base fields correctly", func() {
   433  				Expect(rh.ObjectType()).To(Equal("record:host"))
   434  				Expect(rh.ReturnFields()).To(ConsistOf("extattrs", "ipv4addrs", "name", "view", "zone"))
   435  			})
   436  		})
   437  
   438  		Context("RecordTXT object", func() {
   439  			name := "txt.domain.com"
   440  			text := "this is text string"
   441  			view := "default"
   442  			zone := "domain.com"
   443  
   444  			rt := NewRecordTXT(RecordTXT{
   445  				Name: name,
   446  				Text: text,
   447  				View: view,
   448  				Zone: zone})
   449  
   450  			It("should set fields correctly", func() {
   451  				Expect(rt.Name).To(Equal(name))
   452  				Expect(rt.Text).To(Equal(text))
   453  				Expect(rt.View).To(Equal(view))
   454  				Expect(rt.Zone).To(Equal(zone))
   455  			})
   456  
   457  			It("should set base fields correctly", func() {
   458  				Expect(rt.ObjectType()).To(Equal("record:txt"))
   459  				Expect(rt.ReturnFields()).To(ConsistOf("extattrs", "name", "text", "view", "zone", "ttl", "use_ttl"))
   460  			})
   461  		})
   462  
   463  		Context("ZoneAuth object", func() {
   464  			fqdn := "domain.com"
   465  			view := "default"
   466  
   467  			za := NewZoneAuth(ZoneAuth{
   468  				Fqdn: fqdn,
   469  				View: view})
   470  
   471  			It("should set fields correctly", func() {
   472  				Expect(za.Fqdn).To(Equal(fqdn))
   473  				Expect(za.View).To(Equal(view))
   474  			})
   475  
   476  			It("should set base fields correctly", func() {
   477  				Expect(za.ObjectType()).To(Equal("zone_auth"))
   478  				Expect(za.ReturnFields()).To(ConsistOf("extattrs", "fqdn", "view"))
   479  			})
   480  		})
   481  
   482  		Context("ZoneDelegated object", func() {
   483  			fqdn := "delegated_zone.domain.com"
   484  			view := "default"
   485  
   486  			za := NewZoneDelegated(ZoneDelegated{
   487  				Fqdn: fqdn,
   488  				View: view})
   489  
   490  			It("should set fields correctly", func() {
   491  				Expect(za.Fqdn).To(Equal(fqdn))
   492  				Expect(za.View).To(Equal(view))
   493  			})
   494  
   495  			It("should set base fields correctly", func() {
   496  				Expect(za.ObjectType()).To(Equal("zone_delegated"))
   497  				Expect(za.ReturnFields()).To(ConsistOf("extattrs", "fqdn", "view", "delegate_to"))
   498  			})
   499  		})
   500  
   501  	})
   502  
   503  	Context("Unmarshalling malformed JSON", func() {
   504  		Context("for EA", func() {
   505  			badJSON := `""`
   506  			var ea EA
   507  			err := json.Unmarshal([]byte(badJSON), &ea)
   508  
   509  			It("should return an error", func() {
   510  				Expect(err).ToNot(BeNil())
   511  			})
   512  		})
   513  
   514  		Context("for EADefListValue", func() {
   515  			badJSON := `""`
   516  			var ead EADefListValue
   517  			err := json.Unmarshal([]byte(badJSON), &ead)
   518  
   519  			It("should return an error", func() {
   520  				Expect(err).ToNot(BeNil())
   521  			})
   522  		})
   523  
   524  	})
   525  
   526  })