How to use Count method of vm Package

Best Syzkaller code snippet using vm.Count

create_vm_test.go

Source:create_vm_test.go Github

copy

Full Screen

...197 })198 It("creates the vm when deployByBoshCli=true", func() {199 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)200 Expect(err).NotTo(HaveOccurred())201 Expect(imageService.FindCallCount()).To(Equal(1))202 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))203 Expect(vmService.GetVlanCallCount()).To(Equal(1))204 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))205 Expect(vmService.ReloadOSCallCount()).To(Equal(1))206 Expect(vmService.CreateCallCount()).To(Equal(0))207 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))208 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))209 Expect(vmService.CleanUpCallCount()).To(Equal(0))210 Expect(registryClient.UpdateCalled).To(BeTrue())211 Expect(vmService.FindCallCount()).To(Equal(1))212 Expect(registryClient.UpdateSettings).To(Equal(expectedAgentSettings))213 actualCid, _ := vmService.ConfigureNetworksArgsForCall(0)214 Expect(vmCID).To(Equal(VMCID(actualCid).String()))215 _, actualInstanceNetworks := vmService.ConfigureNetworksArgsForCall(0)216 Expect(actualInstanceNetworks).To(Equal(expectedInstanceNetworks))217 })218 It("After creating the vm with manual network, local /etc/hosts updated", func() {219 networks["fake-manual-network"] = Network{220 Type: "manual",221 IP: "100.10.10.123",222 Gateway: "fake-network-gateway",223 Netmask: "fake-network-netmask",224 DNS: []string{"fake-network-dns"},225 Default: []string{"fake-network-default"},226 CloudProperties: NetworkCloudProperties{227 VlanIds: []int{42345678},228 },229 }230 expectedInstanceNetworks = networks.AsInstanceServiceNetworks(&datatypes.Network_Vlan{})231 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)232 Expect(err).NotTo(HaveOccurred())233 Expect(imageService.FindCallCount()).To(Equal(1))234 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))235 Expect(vmService.GetVlanCallCount()).To(Equal(1))236 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))237 Expect(vmService.ReloadOSCallCount()).To(Equal(1))238 Expect(vmService.CreateCallCount()).To(Equal(0))239 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))240 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))241 Expect(vmService.CleanUpCallCount()).To(Equal(0))242 Expect(registryClient.UpdateCalled).To(BeTrue())243 Expect(registryClient.UpdateSettings).To(Equal(expectedAgentSettings))244 actualCid, _ := vmService.ConfigureNetworksArgsForCall(0)245 Expect(vmCID).To(Equal(VMCID(actualCid).String()))246 _, actualInstanceNetworks := vmService.ConfigureNetworksArgsForCall(0)247 Expect(actualInstanceNetworks).To(Equal(expectedInstanceNetworks))248 })249 It("creates the vm when deployByBoshCli=false and director host is hostname", func() {250 cloudProps = VMCloudProperties{251 HostnamePrefix: "fake-hostname",252 Domain: "fake-domain.com",253 Cpu: 2,254 Memory: 2048,255 MaxNetworkSpeed: 100,256 Datacenter: "fake-datacenter",257 SshKey: 32345678,258 }259 agentOptions = registry.AgentOptions{260 Mbus: "nats://nats:nats@fake-hostname:1234",261 Blobstore: registry.BlobstoreOptions{262 Provider: "dav",263 Options: map[string]interface{}{"endpoint": "http://fake-hostname:1234"},264 },265 }266 addrs, err := net.InterfaceAddrs()267 Expect(err).NotTo(HaveOccurred())268 var localIpAddr string269 for _, addr := range addrs {270 if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {271 if ipnet.IP.To4() != nil {272 localIpAddr = ipnet.IP.String()273 break274 }275 }276 }277 expectedAgentSettings = registry.AgentSettings{278 AgentID: "fake-agent-id",279 Blobstore: registry.BlobstoreSettings{280 Provider: "dav",281 Options: map[string]interface{}{"endpoint": fmt.Sprintf("http://%s:1234", localIpAddr)},282 },283 Disks: registry.DisksSettings{284 System: "",285 Persistent: map[string]registry.PersistentSettings{},286 },287 Mbus: fmt.Sprintf("nats://nats:nats@%s:1234", localIpAddr),288 Networks: registry.NetworksSettings{289 "fake-network-name": registry.NetworkSettings{290 Type: "dynamic",291 IP: "10.10.10.10",292 Gateway: "fake-network-gateway",293 Netmask: "fake-network-netmask",294 DNS: []string{"fake-network-dns"},295 Default: []string{"fake-network-default"},296 },297 },298 Env: registry.EnvSettings(map[string]interface{}{299 "bosh": map[string]interface{}{300 "keep_root_password": true,301 "groups": []interface{}{"fake-tag"},302 },303 }),304 VM: registry.VMSettings{305 Name: "52345678",306 },307 }308 createVM = NewCreateVM(309 imageService,310 vmService,311 registryClient,312 registryOptions,313 agentOptions,314 softlayerOptions,315 localDNSConfigFile,316 )317 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)318 Expect(err).NotTo(HaveOccurred())319 Expect(imageService.FindCallCount()).To(Equal(1))320 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))321 Expect(vmService.GetVlanCallCount()).To(Equal(1))322 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))323 Expect(vmService.ReloadOSCallCount()).To(Equal(1))324 Expect(vmService.CreateCallCount()).To(Equal(0))325 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))326 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))327 Expect(vmService.CleanUpCallCount()).To(Equal(0))328 Expect(registryClient.UpdateCalled).To(BeTrue())329 Expect(vmService.FindCallCount()).To(Equal(1))330 Expect(registryClient.UpdateSettings).To(Equal(expectedAgentSettings))331 actualCid, _ := vmService.ConfigureNetworksArgsForCall(0)332 Expect(vmCID).To(Equal(VMCID(actualCid).String()))333 _, actualInstanceNetworks := vmService.ConfigureNetworksArgsForCall(0)334 Expect(actualInstanceNetworks).To(Equal(expectedInstanceNetworks))335 })336 It("creates the vm when deployByBoshCli=false and director host is IP address", func() {337 cloudProps = VMCloudProperties{338 HostnamePrefix: "fake-hostname",339 Domain: "fake-domain.com",340 Cpu: 2,341 Memory: 2048,342 MaxNetworkSpeed: 100,343 Datacenter: "fake-datacenter",344 SshKey: 32345678,345 }346 agentOptions = registry.AgentOptions{347 Mbus: "nats://nats:nats@10.11.12.13:1234",348 Blobstore: registry.BlobstoreOptions{349 Provider: "dav",350 Options: map[string]interface{}{"endpoint": "http://10.11.12.13:1234"},351 },352 }353 expectedAgentSettings = registry.AgentSettings{354 AgentID: "fake-agent-id",355 Blobstore: registry.BlobstoreSettings{356 Provider: "dav",357 Options: map[string]interface{}{"endpoint": "http://10.11.12.13:1234"},358 },359 Disks: registry.DisksSettings{360 System: "",361 Persistent: map[string]registry.PersistentSettings{},362 },363 Mbus: "nats://nats:nats@10.11.12.13:1234",364 Networks: registry.NetworksSettings{365 "fake-network-name": registry.NetworkSettings{366 Type: "dynamic",367 IP: "10.10.10.10",368 Gateway: "fake-network-gateway",369 Netmask: "fake-network-netmask",370 DNS: []string{"fake-network-dns"},371 Default: []string{"fake-network-default"},372 },373 },374 Env: registry.EnvSettings(map[string]interface{}{375 "bosh": map[string]interface{}{376 "keep_root_password": true,377 "groups": []interface{}{"fake-tag"},378 },379 }),380 VM: registry.VMSettings{381 Name: "52345678",382 },383 }384 createVM = NewCreateVM(385 imageService,386 vmService,387 registryClient,388 registryOptions,389 agentOptions,390 softlayerOptions,391 localDNSConfigFile,392 )393 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)394 Expect(err).NotTo(HaveOccurred())395 Expect(imageService.FindCallCount()).To(Equal(1))396 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))397 Expect(vmService.GetVlanCallCount()).To(Equal(1))398 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))399 Expect(vmService.ReloadOSCallCount()).To(Equal(1))400 Expect(vmService.CreateCallCount()).To(Equal(0))401 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))402 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))403 Expect(vmService.CleanUpCallCount()).To(Equal(0))404 Expect(registryClient.UpdateCalled).To(BeTrue())405 Expect(vmService.FindCallCount()).To(Equal(1))406 Expect(registryClient.UpdateSettings).To(Equal(expectedAgentSettings))407 actualCid, _ := vmService.ConfigureNetworksArgsForCall(0)408 Expect(vmCID).To(Equal(VMCID(actualCid).String()))409 _, actualInstanceNetworks := vmService.ConfigureNetworksArgsForCall(0)410 Expect(actualInstanceNetworks).To(Equal(expectedInstanceNetworks))411 })412 It("creates the vm successfully when length of hostname is 64", func() {413 cloudProps = VMCloudProperties{414 HostnamePrefix: "fake-randomstring-346e9mlcy90i4n57oc0zk-hostname",415 Domain: "fake-domain.com",416 Cpu: 2,417 Memory: 2048,418 MaxNetworkSpeed: 100,419 Datacenter: "fake-datacenter",420 SshKey: 32345678,421 DeployedByBoshCLI: true,422 }423 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)424 Expect(err).NotTo(HaveOccurred())425 Expect(imageService.FindCallCount()).To(Equal(1))426 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))427 Expect(vmService.GetVlanCallCount()).To(Equal(1))428 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))429 Expect(vmService.ReloadOSCallCount()).To(Equal(1))430 Expect(vmService.CreateCallCount()).To(Equal(0))431 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))432 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))433 Expect(vmService.CleanUpCallCount()).To(Equal(0))434 Expect(registryClient.UpdateCalled).To(BeTrue())435 Expect(vmService.FindCallCount()).To(Equal(1))436 Expect(registryClient.UpdateSettings).To(Equal(expectedAgentSettings))437 actualCid, _ := vmService.ConfigureNetworksArgsForCall(0)438 Expect(vmCID).To(Equal(VMCID(actualCid).String()))439 _, actualInstanceNetworks := vmService.ConfigureNetworksArgsForCall(0)440 Expect(actualInstanceNetworks).To(Equal(expectedInstanceNetworks))441 })442 It("returns an error if imageService find call returns an error", func() {443 imageService.FindReturns(444 "12345678",445 errors.New("fake-image-service-error"),446 )447 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)448 Expect(err).To(HaveOccurred())449 Expect(err.Error()).To(ContainSubstring("fake-image-service-error"))450 Expect(imageService.FindCallCount()).To(Equal(1))451 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))452 Expect(vmService.GetVlanCallCount()).To(Equal(0))453 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(0))454 Expect(vmService.ReloadOSCallCount()).To(Equal(0))455 Expect(vmService.CreateCallCount()).To(Equal(0))456 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(0))457 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))458 Expect(vmService.CleanUpCallCount()).To(Equal(0))459 Expect(registryClient.UpdateCalled).To(BeFalse())460 })461 It("returns an error if imageService find call returns an api error", func() {462 imageService.FindReturns(463 "12345678",464 api.NewStemcellkNotFoundError(stemcellCID.String(), false),465 )466 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)467 Expect(err).To(HaveOccurred())468 Expect(err.Error()).To(ContainSubstring(fmt.Sprintf("Stemcell '%s' not found", stemcellCID.String())))469 Expect(imageService.FindCallCount()).To(Equal(1))470 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))471 Expect(vmService.GetVlanCallCount()).To(Equal(0))472 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(0))473 Expect(vmService.ReloadOSCallCount()).To(Equal(0))474 Expect(vmService.CreateCallCount()).To(Equal(0))475 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(0))476 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))477 Expect(vmService.CleanUpCallCount()).To(Equal(0))478 Expect(registryClient.UpdateCalled).To(BeFalse())479 })480 It("returns an error if vmService get vlan call returns an error", func() {481 vmService.GetVlanReturns(482 &datatypes.Network_Vlan{},483 errors.New("fake-vm-service-error"),484 )485 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)486 Expect(err).To(HaveOccurred())487 Expect(err.Error()).To(ContainSubstring("fake-vm-service-error"))488 Expect(imageService.FindCallCount()).To(Equal(1))489 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))490 Expect(vmService.GetVlanCallCount()).To(Equal(1))491 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(0))492 Expect(vmService.ReloadOSCallCount()).To(Equal(0))493 Expect(vmService.CreateCallCount()).To(Equal(0))494 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(0))495 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))496 Expect(vmService.CleanUpCallCount()).To(Equal(0))497 Expect(registryClient.UpdateCalled).To(BeFalse())498 })499 It("returns an error if vmService find by primary backend ip call returns an error", func() {500 vmService.FindByPrimaryBackendIpReturns(501 &datatypes.Virtual_Guest{},502 errors.New("fake-vm-service-error"),503 )504 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)505 Expect(err).To(HaveOccurred())506 Expect(err.Error()).To(ContainSubstring("fake-vm-service-error"))507 Expect(imageService.FindCallCount()).To(Equal(1))508 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))509 Expect(vmService.GetVlanCallCount()).To(Equal(1))510 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))511 Expect(vmService.ReloadOSCallCount()).To(Equal(0))512 Expect(vmService.CreateCallCount()).To(Equal(0))513 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(0))514 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))515 Expect(vmService.CleanUpCallCount()).To(Equal(0))516 Expect(registryClient.UpdateCalled).To(BeFalse())517 })518 It("returns an error if vmService reload os call returns an error", func() {519 vmService.ReloadOSReturns(520 errors.New("fake-vm-service-error"),521 )522 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)523 Expect(err).To(HaveOccurred())524 Expect(err.Error()).To(ContainSubstring("OS reloading VM: fake-vm-service-error"))525 Expect(imageService.FindCallCount()).To(Equal(1))526 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))527 Expect(vmService.GetVlanCallCount()).To(Equal(1))528 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))529 Expect(vmService.ReloadOSCallCount()).To(Equal(1))530 Expect(vmService.CreateCallCount()).To(Equal(0))531 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(0))532 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))533 Expect(vmService.CleanUpCallCount()).To(Equal(0))534 Expect(registryClient.UpdateCalled).To(BeFalse())535 })536 It("returns an error if vmService configure networks returns an error", func() {537 vmService.ConfigureNetworksReturns(538 instance.Networks{},539 errors.New("fake-vm-service-error"),540 )541 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)542 Expect(err).To(HaveOccurred())543 Expect(err.Error()).To(ContainSubstring("fake-vm-service-error"))544 Expect(imageService.FindCallCount()).To(Equal(1))545 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))546 Expect(vmService.GetVlanCallCount()).To(Equal(1))547 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))548 Expect(vmService.ReloadOSCallCount()).To(Equal(1))549 Expect(vmService.CreateCallCount()).To(Equal(0))550 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))551 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))552 Expect(vmService.CleanUpCallCount()).To(Equal(0))553 Expect(registryClient.UpdateCalled).To(BeFalse())554 })555 It("returns an error if registryClient update call returns an error", func() {556 registryClient.UpdateErr = errors.New("fake-registry-client-error")557 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)558 Expect(err).To(HaveOccurred())559 Expect(err.Error()).To(ContainSubstring("fake-registry-client-error"))560 Expect(imageService.FindCallCount()).To(Equal(1))561 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))562 Expect(vmService.GetVlanCallCount()).To(Equal(1))563 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))564 Expect(vmService.ReloadOSCallCount()).To(Equal(1))565 Expect(vmService.CreateCallCount()).To(Equal(0))566 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))567 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))568 Expect(vmService.CleanUpCallCount()).To(Equal(0))569 Expect(registryClient.UpdateCalled).To(BeTrue())570 })571 Context("when softlayer options PublicKey is set", func() {572 BeforeEach(func() {573 softlayerOptions = boslconfig.Config{574 Username: "fake-username",575 ApiKey: "fake-api-key",576 ApiEndpoint: "fake-api-endpoint",577 DisableOsReload: false,578 PublicKey: "fake-public-key",579 PublicKeyFingerPrint: "fake-public-key-fingerprint",580 }581 createVM = NewCreateVM(582 imageService,583 vmService,584 registryClient,585 registryOptions,586 agentOptions,587 softlayerOptions,588 localDNSConfigFile,589 )590 })591 It("creates the vm and create ssh key", func() {592 vmService.CreateSshKeyReturns(593 1234567,594 nil,595 )596 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)597 Expect(err).NotTo(HaveOccurred())598 Expect(imageService.FindCallCount()).To(Equal(1))599 Expect(vmService.CreateSshKeyCallCount()).To(Equal(1))600 Expect(vmService.GetVlanCallCount()).To(Equal(1))601 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))602 Expect(vmService.ReloadOSCallCount()).To(Equal(1))603 Expect(vmService.CreateCallCount()).To(Equal(0))604 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))605 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))606 Expect(vmService.CleanUpCallCount()).To(Equal(0))607 Expect(registryClient.UpdateCalled).To(BeTrue())608 Expect(registryClient.UpdateSettings).To(Equal(expectedAgentSettings))609 actualCid, _ := vmService.ConfigureNetworksArgsForCall(0)610 Expect(vmCID).To(Equal(VMCID(actualCid).String()))611 _, actualInstanceNetworks := vmService.ConfigureNetworksArgsForCall(0)612 Expect(actualInstanceNetworks).To(Equal(expectedInstanceNetworks))613 })614 It("returns an error if vmService create ssh key call returns an error", func() {615 vmService.CreateSshKeyReturns(616 0,617 errors.New("fake-vm-service-error"),618 )619 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)620 Expect(err).To(HaveOccurred())621 Expect(err.Error()).To(ContainSubstring("fake-vm-service-error"))622 Expect(imageService.FindCallCount()).To(Equal(1))623 Expect(vmService.CreateSshKeyCallCount()).To(Equal(1))624 Expect(vmService.GetVlanCallCount()).To(Equal(0))625 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(0))626 Expect(vmService.ReloadOSCallCount()).To(Equal(0))627 Expect(vmService.CreateCallCount()).To(Equal(0))628 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(0))629 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))630 Expect(vmService.CleanUpCallCount()).To(Equal(0))631 Expect(registryClient.UpdateCalled).To(BeFalse())632 })633 })634 Context("when softlayer options DisableOsReload is set", func() {635 BeforeEach(func() {636 softlayerOptions = boslconfig.Config{637 Username: "fake-username",638 ApiKey: "fake-api-key",639 ApiEndpoint: "fake-api-endpoint",640 DisableOsReload: true,641 }642 createVM = NewCreateVM(643 imageService,644 vmService,645 registryClient,646 registryOptions,647 agentOptions,648 softlayerOptions,649 localDNSConfigFile,650 )651 expectedAgentSettings = registry.AgentSettings{652 AgentID: "fake-agent-id",653 Blobstore: registry.BlobstoreSettings{654 Provider: "dav",655 Options: map[string]interface{}{"endpoint": "http://fake-blobstore:1234"},656 },657 Disks: registry.DisksSettings{658 System: "",659 Persistent: map[string]registry.PersistentSettings{},660 },661 Mbus: "nats://nats:nats@fake-mbus:1234",662 Networks: registry.NetworksSettings{663 "fake-network-name": registry.NetworkSettings{664 Type: "dynamic",665 IP: "10.10.10.10",666 Gateway: "fake-network-gateway",667 Netmask: "fake-network-netmask",668 DNS: []string{"fake-network-dns"},669 Default: []string{"fake-network-default"},670 },671 },672 Env: registry.EnvSettings(map[string]interface{}{673 "bosh": map[string]interface{}{674 "keep_root_password": true,675 "groups": []interface{}{"fake-tag"},676 },677 }),678 VM: registry.VMSettings{679 Name: "62345678",680 },681 }682 vmService.CreateReturns(683 62345678,684 nil,685 )686 })687 It("creates the vm with only private network", func() {688 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)689 virtualGuest, _, _, _, _ := vmService.CreateArgsForCall(0)690 actualCid, _ := vmService.ConfigureNetworksArgsForCall(0)691 _, actualInstanceNetworks := vmService.ConfigureNetworksArgsForCall(0)692 Expect(err).NotTo(HaveOccurred())693 Expect(imageService.FindCallCount()).To(Equal(1))694 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))695 Expect(vmService.GetVlanCallCount()).To(Equal(1))696 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(0))697 Expect(vmService.ReloadOSCallCount()).To(Equal(0))698 Expect(*virtualGuest.PrivateNetworkOnlyFlag).To(BeTrue())699 Expect(vmService.CreateCallCount()).To(Equal(1))700 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))701 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))702 Expect(vmService.CleanUpCallCount()).To(Equal(0))703 Expect(registryClient.UpdateCalled).To(BeTrue())704 Expect(registryClient.UpdateSettings).To(Equal(expectedAgentSettings))705 Expect(vmCID).To(Equal(VMCID(actualCid).String()))706 Expect(actualInstanceNetworks).To(Equal(expectedInstanceNetworks))707 })708 It("Failed to create the vm with only public network", func() {709 networks = Networks{710 "fake-network-name": Network{711 Type: "dynamic",712 IP: "10.10.10.10",713 Gateway: "fake-network-gateway",714 Netmask: "fake-network-netmask",715 DNS: []string{"fake-network-dns"},716 Default: []string{"fake-network-default"},717 CloudProperties: NetworkCloudProperties{718 VlanIds: []int{42345680},719 SourcePolicyRouting: true,720 },721 },722 }723 vmService.GetVlanReturns(724 &datatypes.Network_Vlan{725 Id: sl.Int(42345680),726 NetworkSpace: sl.String("PUBLIC"),727 },728 nil,729 )730 _, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)731 Expect(err).To(HaveOccurred())732 Expect(err.Error()).To(ContainSubstring("A private network is required"))733 Expect(imageService.FindCallCount()).To(Equal(1))734 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))735 Expect(vmService.GetVlanCallCount()).To(Equal(1))736 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(0))737 Expect(vmService.ReloadOSCallCount()).To(Equal(0))738 Expect(vmService.CreateCallCount()).To(Equal(0))739 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(0))740 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))741 Expect(vmService.CleanUpCallCount()).To(Equal(0))742 Expect(registryClient.UpdateCalled).To(BeFalse())743 })744 It("returns an error if vmService create call returns an error", func() {745 vmService.CreateReturns(746 0,747 errors.New("fake-vm-service-error"),748 )749 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)750 Expect(err).To(HaveOccurred())751 Expect(err.Error()).To(ContainSubstring("fake-vm-service-error"))752 Expect(imageService.FindCallCount()).To(Equal(1))753 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))754 Expect(vmService.GetVlanCallCount()).To(Equal(1))755 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(0))756 Expect(vmService.ReloadOSCallCount()).To(Equal(0))757 Expect(vmService.CreateCallCount()).To(Equal(1))758 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(0))759 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))760 Expect(vmService.CleanUpCallCount()).To(Equal(0))761 Expect(registryClient.UpdateCalled).To(BeFalse())762 })763 It("returns an error if vmService create call returns an api error", func() {764 vmService.CreateReturns(765 0,766 api.NewVMCreationFailedError("InternalError", false),767 )768 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)769 Expect(err).To(HaveOccurred())770 Expect(err.Error()).To(ContainSubstring("VM failed to create: InternalError"))771 Expect(imageService.FindCallCount()).To(Equal(1))772 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))773 Expect(vmService.GetVlanCallCount()).To(Equal(1))774 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(0))775 Expect(vmService.ReloadOSCallCount()).To(Equal(0))776 Expect(vmService.CreateCallCount()).To(Equal(1))777 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(0))778 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))779 Expect(vmService.CleanUpCallCount()).To(Equal(0))780 Expect(registryClient.UpdateCalled).To(BeFalse())781 })782 It("returns an error and cleans up if vmService configure networks returns an error", func() {783 vmService.ConfigureNetworksReturns(784 instance.Networks{},785 errors.New("fake-vm-service-error"),786 )787 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)788 Expect(err).To(HaveOccurred())789 Expect(err.Error()).To(ContainSubstring("fake-vm-service-error"))790 Expect(imageService.FindCallCount()).To(Equal(1))791 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))792 Expect(vmService.GetVlanCallCount()).To(Equal(1))793 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(0))794 Expect(vmService.ReloadOSCallCount()).To(Equal(0))795 Expect(vmService.CreateCallCount()).To(Equal(1))796 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))797 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))798 Expect(vmService.CleanUpCallCount()).To(Equal(1))799 Expect(registryClient.UpdateCalled).To(BeFalse())800 })801 It("returns an error and cleans up if registryClient update call returns an error", func() {802 registryClient.UpdateErr = errors.New("fake-registry-client-error")803 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)804 Expect(err).To(HaveOccurred())805 Expect(err.Error()).To(ContainSubstring("fake-registry-client-error"))806 Expect(imageService.FindCallCount()).To(Equal(1))807 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))808 Expect(vmService.GetVlanCallCount()).To(Equal(1))809 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(0))810 Expect(vmService.ReloadOSCallCount()).To(Equal(0))811 Expect(vmService.CreateCallCount()).To(Equal(1))812 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))813 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))814 Expect(vmService.CleanUpCallCount()).To(Equal(1))815 Expect(registryClient.UpdateCalled).To(BeTrue())816 })817 })818 Context("when cloud property options EphemeralDiskSize is set", func() {819 BeforeEach(func() {820 expectedAgentSettings = registry.AgentSettings{821 AgentID: "fake-agent-id",822 Blobstore: registry.BlobstoreSettings{823 Provider: "dav",824 Options: map[string]interface{}{"endpoint": "http://fake-blobstore:1234"},825 },826 Disks: registry.DisksSettings{827 System: "",828 Ephemeral: "/dev/xvdc",829 Persistent: map[string]registry.PersistentSettings{},830 },831 Mbus: "nats://nats:nats@fake-mbus:1234",832 Networks: registry.NetworksSettings{833 "fake-network-name": registry.NetworkSettings{834 Type: "dynamic",835 IP: "10.10.10.10",836 Gateway: "fake-network-gateway",837 Netmask: "fake-network-netmask",838 DNS: []string{"fake-network-dns"},839 Default: []string{"fake-network-default"},840 },841 },842 Env: registry.EnvSettings(map[string]interface{}{"bosh": map[string]interface{}{"keep_root_password": true, "groups": []interface{}{"fake-tag"}}}),843 VM: registry.VMSettings{844 Name: "52345678",845 },846 }847 cloudProps = VMCloudProperties{848 HostnamePrefix: "fake-hostname",849 Domain: "fake-domain.com",850 Cpu: 2,851 Memory: 2048,852 MaxNetworkSpeed: 100,853 Datacenter: "fake-datacenter",854 SshKey: 32345678,855 EphemeralDiskSize: 2048,856 DeployedByBoshCLI: true,857 }858 })859 It("creates the vm and attaches ephemeral disk", func() {860 vmService.AttachEphemeralDiskReturns(861 nil,862 )863 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)864 Expect(err).NotTo(HaveOccurred())865 Expect(imageService.FindCallCount()).To(Equal(1))866 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))867 Expect(vmService.GetVlanCallCount()).To(Equal(1))868 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))869 Expect(vmService.ReloadOSCallCount()).To(Equal(1))870 Expect(vmService.CreateCallCount()).To(Equal(0))871 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))872 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(1))873 Expect(registryClient.UpdateCalled).To(BeTrue())874 Expect(vmService.CleanUpCallCount()).To(Equal(0))875 Expect(registryClient.UpdateSettings).To(Equal(expectedAgentSettings))876 actualCid, _ := vmService.ConfigureNetworksArgsForCall(0)877 Expect(vmCID).To(Equal(VMCID(actualCid).String()))878 _, actualInstanceNetworks := vmService.ConfigureNetworksArgsForCall(0)879 Expect(actualInstanceNetworks).To(Equal(expectedInstanceNetworks))880 })881 It("returns an error if vmService attach ephemeral disk call returns an error", func() {882 vmService.AttachEphemeralDiskReturns(883 errors.New("fake-vm-service-error"),884 )885 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)886 Expect(err).To(HaveOccurred())887 Expect(err.Error()).To(ContainSubstring("fake-vm-service-error"))888 Expect(imageService.FindCallCount()).To(Equal(1))889 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))890 Expect(vmService.GetVlanCallCount()).To(Equal(1))891 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))892 Expect(vmService.ReloadOSCallCount()).To(Equal(1))893 Expect(vmService.CreateCallCount()).To(Equal(0))894 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))895 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(1))896 Expect(registryClient.UpdateCalled).To(BeFalse())897 Expect(vmService.CleanUpCallCount()).To(Equal(0))898 })899 })900 Context("when required cloud properties is not set", func() {901 It("returns an error if property 'vmNamePrefix' is not set", func() {902 cloudProps = VMCloudProperties{903 HostnamePrefix: "",904 Domain: "fake-domain.com",905 Cpu: 2,906 Memory: 2048,907 MaxNetworkSpeed: 100,908 Datacenter: "fake-datacenter",909 SshKey: 32345678,910 DeployedByBoshCLI: true,911 }912 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)913 Expect(err).To(HaveOccurred())914 Expect(err.Error()).To(ContainSubstring("The property 'hostname_prefix' must be set to create an instance"))915 Expect(imageService.FindCallCount()).To(Equal(0))916 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))917 Expect(vmService.GetVlanCallCount()).To(Equal(0))918 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(0))919 Expect(vmService.ReloadOSCallCount()).To(Equal(0))920 Expect(vmService.CreateCallCount()).To(Equal(0))921 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(0))922 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))923 Expect(vmService.CleanUpCallCount()).To(Equal(0))924 Expect(registryClient.UpdateCalled).To(BeFalse())925 })926 It("returns an error if property 'datacenter' is not set", func() {927 cloudProps = VMCloudProperties{928 HostnamePrefix: "fake-hostname",929 Domain: "fake-domain.com",930 Cpu: 2,931 Memory: 2048,932 MaxNetworkSpeed: 100,933 Datacenter: "",934 SshKey: 32345678,935 DeployedByBoshCLI: true,936 }937 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)938 Expect(err).To(HaveOccurred())939 Expect(err.Error()).To(ContainSubstring("The property 'datacenter' must be set to create an instance"))940 Expect(imageService.FindCallCount()).To(Equal(0))941 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))942 Expect(vmService.GetVlanCallCount()).To(Equal(0))943 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(0))944 Expect(vmService.ReloadOSCallCount()).To(Equal(0))945 Expect(vmService.CreateCallCount()).To(Equal(0))946 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(0))947 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))948 Expect(vmService.CleanUpCallCount()).To(Equal(0))949 Expect(registryClient.UpdateCalled).To(BeFalse())950 })951 })952 Context("when vcap password exists in both env.bosh and agent option", func() {953 BeforeEach(func() {954 agentOptions = registry.AgentOptions{955 Mbus: "nats://nats:nats@fake-mbus:1234",956 Blobstore: registry.BlobstoreOptions{957 Provider: "dav",958 Options: map[string]interface{}{"endpoint": "http://fake-blobstore:1234"},959 },960 VcapPassword: "fake-vcap-password-in-agent",961 }962 env = Environment(map[string]interface{}{"bosh": map[string]interface{}{"keep_root_password": false, "groups": []interface{}{"fake-tag"}}})963 env["bosh"].(map[string]interface{})["password"] = "fake-vcap-password-in-env"964 createVM = NewCreateVM(965 imageService,966 vmService,967 registryClient,968 registryOptions,969 agentOptions,970 softlayerOptions,971 localDNSConfigFile,972 )973 expectedAgentSettings = registry.AgentSettings{974 AgentID: "fake-agent-id",975 Blobstore: registry.BlobstoreSettings{976 Provider: "dav",977 Options: map[string]interface{}{"endpoint": "http://fake-blobstore:1234"},978 },979 Disks: registry.DisksSettings{980 System: "",981 Persistent: map[string]registry.PersistentSettings{},982 },983 Mbus: "nats://nats:nats@fake-mbus:1234",984 Networks: registry.NetworksSettings{985 "fake-network-name": registry.NetworkSettings{986 Type: "dynamic",987 IP: "10.10.10.10",988 Gateway: "fake-network-gateway",989 Netmask: "fake-network-netmask",990 DNS: []string{"fake-network-dns"},991 Default: []string{"fake-network-default"},992 },993 },994 Env: registry.EnvSettings(map[string]interface{}{995 "bosh": map[string]interface{}{996 "keep_root_password": true,997 "groups": []interface{}{"fake-tag"},998 "password": "fake-vcap-password-in-env",999 },1000 }),1001 VM: registry.VMSettings{1002 Name: "52345678",1003 },1004 }1005 })1006 It("do not change vcap password in env.bosh", func() {1007 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)1008 Expect(err).NotTo(HaveOccurred())1009 Expect(imageService.FindCallCount()).To(Equal(1))1010 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))1011 Expect(vmService.GetVlanCallCount()).To(Equal(1))1012 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))1013 Expect(vmService.ReloadOSCallCount()).To(Equal(1))1014 Expect(vmService.CreateCallCount()).To(Equal(0))1015 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))1016 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))1017 Expect(vmService.CleanUpCallCount()).To(Equal(0))1018 Expect(registryClient.UpdateCalled).To(BeTrue())1019 Expect(vmService.FindCallCount()).To(Equal(1))1020 Expect(registryClient.UpdateSettings).To(BeEquivalentTo(expectedAgentSettings))1021 actualCid, _ := vmService.ConfigureNetworksArgsForCall(0)1022 Expect(vmCID).To(Equal(VMCID(actualCid).String()))1023 _, actualInstanceNetworks := vmService.ConfigureNetworksArgsForCall(0)1024 Expect(actualInstanceNetworks).To(BeEquivalentTo(expectedInstanceNetworks))1025 })1026 })1027 Context("when env.bosh exists but does not have vcap password, vcap password exists in agent option", func() {1028 BeforeEach(func() {1029 agentOptions = registry.AgentOptions{1030 Mbus: "nats://nats:nats@fake-mbus:1234",1031 Blobstore: registry.BlobstoreOptions{1032 Provider: "dav",1033 Options: map[string]interface{}{"endpoint": "http://fake-blobstore:1234"},1034 },1035 VcapPassword: "fake-vcap-password-in-agent",1036 }1037 env = Environment(map[string]interface{}{"bosh": map[string]interface{}{"keep_root_password": false,1038 "groups": []interface{}{"fake-tag"}}})1039 createVM = NewCreateVM(1040 imageService,1041 vmService,1042 registryClient,1043 registryOptions,1044 agentOptions,1045 softlayerOptions,1046 localDNSConfigFile,1047 )1048 expectedAgentSettings = registry.AgentSettings{1049 AgentID: "fake-agent-id",1050 Blobstore: registry.BlobstoreSettings{1051 Provider: "dav",1052 Options: map[string]interface{}{"endpoint": "http://fake-blobstore:1234"},1053 },1054 Disks: registry.DisksSettings{1055 System: "",1056 Persistent: map[string]registry.PersistentSettings{},1057 },1058 Mbus: "nats://nats:nats@fake-mbus:1234",1059 Networks: registry.NetworksSettings{1060 "fake-network-name": registry.NetworkSettings{1061 Type: "dynamic",1062 IP: "10.10.10.10",1063 Gateway: "fake-network-gateway",1064 Netmask: "fake-network-netmask",1065 DNS: []string{"fake-network-dns"},1066 Default: []string{"fake-network-default"},1067 },1068 },1069 Env: registry.EnvSettings(map[string]interface{}{1070 "bosh": map[string]interface{}{1071 "keep_root_password": true,1072 "groups": []interface{}{"fake-tag"},1073 "password": "fake-vcap-password-in-agent",1074 },1075 }),1076 VM: registry.VMSettings{1077 Name: "52345678",1078 },1079 }1080 })1081 It("do not change vcap password in env.bosh", func() {1082 vmCID, err = createVM.Run(agentID, stemcellCID, cloudProps, networks, disks, env)1083 Expect(err).NotTo(HaveOccurred())1084 Expect(imageService.FindCallCount()).To(Equal(1))1085 Expect(vmService.CreateSshKeyCallCount()).To(Equal(0))1086 Expect(vmService.GetVlanCallCount()).To(Equal(1))1087 Expect(vmService.FindByPrimaryBackendIpCallCount()).To(Equal(1))1088 Expect(vmService.ReloadOSCallCount()).To(Equal(1))1089 Expect(vmService.CreateCallCount()).To(Equal(0))1090 Expect(vmService.ConfigureNetworksCallCount()).To(Equal(1))1091 Expect(vmService.AttachEphemeralDiskCallCount()).To(Equal(0))1092 Expect(vmService.CleanUpCallCount()).To(Equal(0))1093 Expect(registryClient.UpdateCalled).To(BeTrue())1094 Expect(vmService.FindCallCount()).To(Equal(1))1095 Expect(registryClient.UpdateSettings).To(BeEquivalentTo(expectedAgentSettings))1096 actualCid, _ := vmService.ConfigureNetworksArgsForCall(0)1097 Expect(vmCID).To(Equal(VMCID(actualCid).String()))1098 _, actualInstanceNetworks := vmService.ConfigureNetworksArgsForCall(0)1099 Expect(actualInstanceNetworks).To(BeEquivalentTo(expectedInstanceNetworks))1100 })1101 })1102 })1103})...

Full Screen

Full Screen

ari-voicemail-main.go

Source:ari-voicemail-main.go Github

copy

Full Screen

...21 ChannelID string22 ActiveRecording string23 ActivePlaybacks []string24 MailboxProvided bool25 NewMessageCount int26 OldMessageCount int27}28/* Voicemail Users29id (auto)30mailbox (varchar)31domain (varchar)32pin (varchar)33email (varchar)34first_name (varchar)35last_name (varchar)36*/37/* Voicemail Messages38id (auto)39mailbox (varchar)40domain (varchar)41folder (varchar)42timestamp (date)43read (bool)44recording_id (varchar)45*/46/* Voicemail Main47authenticate:48 voicemail_box49 pin50 check if valid51 true: func(leave), goto(main)52 false: decrement tries, goto(authenticate)53main:54 if new messages exist55 true: play(you have new messages)56 false: continue571: new messages582: change folders59 0 new60 1 old61 2 work62 3 family63 4 friends64 # cancel653: advanced options66 4 outgoing call67 enter message to call, then press pound, * to cancel68 5 leave a message69 1 extension70 2 directory (won't implement)710: mailbox options72 1 unavailable msg73 2 busy msg74 3 name75 4 temporary greeting76 5 password77 * main menu78*: help79#: exit80*/81// New stuff that we need to figure out and clean up82type vmMainStateFunc func(a *ari.AppInstance, vmState *vmMainInternal) (vmMainStateFunc, *vmMainInternal)83// AddPlayback adds a playback to the string slice that holds the list of file IDs of the in-flight playback files.84func (v *vmMainInternal) AddPlayback(id string) {85 v.ActivePlaybacks = append(v.ActivePlaybacks, id)86}87// RemovePlayback removes the file ID of the in-flight playback files as they have been played to the channel.88func (v *vmMainInternal) RemovePlayback(id string) {89 for i := range v.ActivePlaybacks {90 if v.ActivePlaybacks[i] == id {91 v.ActivePlaybacks = append(v.ActivePlaybacks[:i], v.ActivePlaybacks[i+1:]...)92 return93 }94 }95}96// PlaybacksStop stops all of the active playbacks and removes them from the list of active playbacks.97func (v *vmMainInternal) PlaybacksStop(a *ari.AppInstance) {98 for _, val := range v.ActivePlaybacks {99 a.PlaybacksStop(val)100 v.RemovePlayback(val)101 }102}103// startVMMainApp starts the primary voicemail application for retrieving messages.104func startVMMainApp(app string) {105 fmt.Printf("Started application: %s\n", app)106 application := new(ari.App)107 application.Init(app, startVMMainHandler)108 select {109 case <-application.Stop:110 return111 }112}113// startVMMainHandler initializes a new voicemail main application instance114func startVMMainHandler(a *ari.AppInstance) {115 v := &vmMainInternal{Retries: 0, Mailbox: "", Domain: "", Password: "", CurrentFolder: "New", MailboxProvided: false}116 state, vmMainState := vmMainStartState(a, v)117 for state != nil {118 state, vmMainState = state(a, vmMainState)119 }120 fmt.Println("exiting app instance")121}122func vmMainStartState(a *ari.AppInstance, vmMainState *vmMainInternal) (vmMainStateFunc, *vmMainInternal) {123 select {124 case event := <-a.Events:125 switch event.Type {126 case "StasisStart":127 fmt.Println("Got start message")128 var s ari.StasisStart129 json.Unmarshal([]byte(event.ARI_Body), &s)130 vmMainState.ChannelID = s.Channel.Id131 a.ChannelsAnswer(vmMainState.ChannelID)132 fmt.Printf("Args is: %s\n", s.Args)133 for i, v := range s.Args {134 switch i {135 case 0:136 fmt.Println("Case 0")137 if v != "" {138 fmt.Printf("Domain is: %s\n", v)139 vmMainState.Domain = strings.TrimSpace(v)140 } else {141 return nil, vmMainState142 }143 case 1:144 fmt.Println("Case 1")145 if v != "" {146 fmt.Printf("Mailbox is: %s\n", v)147 vmMainState.Mailbox = strings.TrimSpace(v)148 vmMainState.MailboxProvided = true149 } else {150 pb, _ := a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-login")151 vmMainState.AddPlayback(pb.Id)152 return acceptMbox, vmMainState153 }154 }155 }156 if vmMainState.Domain == "" {157 return hangupVMMain, vmMainState158 }159 if vmMainState.Mailbox != "" && vmMainState.Domain != "" {160 a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-password")161 return acceptPassword, vmMainState162 } else {163 a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-login")164 return acceptMbox, vmMainState165 }166 }167 }168 return vmMainStartState, vmMainState169}170func acceptMbox(a *ari.AppInstance, vmMainState *vmMainInternal) (vmMainStateFunc, *vmMainInternal) {171 select {172 case event := <-a.Events:173 switch event.Type {174 case "ChannelDtmfReceived":175 var c ari.ChannelDtmfReceived176 json.Unmarshal([]byte(event.ARI_Body), &c)177 switch c.Digit {178 case "#":179 a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-password")180 return acceptPassword, vmMainState181 default:182 vmMainState.Mailbox = strings.Join([]string{vmMainState.Mailbox, c.Digit}, "")183 return acceptMbox, vmMainState184 }185 }186 }187 return acceptMbox, vmMainState188}189func acceptPassword(a *ari.AppInstance, vmMainState *vmMainInternal) (vmMainStateFunc, *vmMainInternal) {190 select {191 case event := <-a.Events:192 switch event.Type {193 case "ChannelDtmfReceived":194 var c ari.ChannelDtmfReceived195 json.Unmarshal([]byte(event.ARI_Body), &c)196 switch c.Digit {197 case "#":198 if authorizeUser(vmMainState.Mailbox, vmMainState.Domain, vmMainState.Password) {199 vmMainState.Retries = 0200 return vmMainMenuIntro, vmMainState201 } else {202 vmMainState.Retries++203 vmMainState.Password = ""204 if vmMainState.Retries < 3 {205 if vmMainState.MailboxProvided {206 a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-incorrect")207 a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-password")208 return acceptPassword, vmMainState209 } else {210 a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-incorrect-mailbox")211 vmMainState.Mailbox = ""212 return acceptMbox, vmMainState213 }214 } else {215 a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-goodbye")216 return hangupVMMain, vmMainState217 }218 }219 default:220 vmMainState.Password = strings.Join([]string{vmMainState.Password, c.Digit}, "")221 fmt.Printf("Password is now: %s\n", vmMainState.Password)222 return acceptPassword, vmMainState223 }224 }225 }226 return acceptPassword, vmMainState227}228func hangupVMMain(a *ari.AppInstance, vmMainState *vmMainInternal) (vmMainStateFunc, *vmMainInternal) {229 time.Sleep(3 * time.Second)230 a.ChannelsHangup(vmMainState.ChannelID)231 return nil, vmMainState232}233func (v *vmMainInternal) PlayMessageCounts(a *ari.AppInstance) {234 curFolder := v.CurrentFolder235 v.NewMessageCount = getMessageCount(v.Mailbox, v.Domain, "New")236 v.OldMessageCount = getMessageCount(v.Mailbox, v.Domain, "Old")237 pb, _ := a.ChannelsPlay(v.ChannelID, "sound:vm-youhave")238 v.AddPlayback(pb.Id)239 if !(v.NewMessageCount > 0 || v.OldMessageCount > 0) {240 pb, _ := a.ChannelsPlay(v.ChannelID, "sound:no")241 v.AddPlayback(pb.Id)242 pb, _ = a.ChannelsPlay(v.ChannelID, "sound:vm-messages")243 v.AddPlayback(pb.Id)244 return245 }246 switch {247 case v.NewMessageCount == 1:248 pb, _ := a.ChannelsPlay(v.ChannelID, "sound:one")249 v.AddPlayback(pb.Id)250 v.PlayFolder(a, "New")251 pb, _ = a.ChannelsPlay(v.ChannelID, "sound:vm-message")252 v.AddPlayback(pb.Id)253 case v.NewMessageCount > 1 :254 pb, _ := a.ChannelsPlay(v.ChannelID, strings.Join([]string{"sound:digits/", strconv.Itoa(v.NewMessageCount)}, ""))255 v.AddPlayback(pb.Id)256 v.PlayFolder(a, "New")257 pb, _ = a.ChannelsPlay(v.ChannelID, "sound:vm-messages")258 v.AddPlayback(pb.Id)259 }260 if v.NewMessageCount != 0 && v.OldMessageCount != 0 {261 pb, _ := a.ChannelsPlay(v.ChannelID, "sound:vm-and")262 v.AddPlayback(pb.Id)263 }264 switch {265 case v.OldMessageCount == 1:266 pb, _ := a.ChannelsPlay(v.ChannelID, "sound:one")267 v.AddPlayback(pb.Id)268 v.PlayFolder(a, "Old")269 pb, _ = a.ChannelsPlay(v.ChannelID, "sound:vm-message")270 v.AddPlayback(pb.Id)271 case v.OldMessageCount > 1 :272 pb, _ := a.ChannelsPlay(v.ChannelID, strings.Join([]string{"sound:digits/", strconv.Itoa(v.OldMessageCount)}, ""))273 v.AddPlayback(pb.Id)274 v.PlayFolder(a, "Old")275 pb, _ = a.ChannelsPlay(v.ChannelID, "sound:vm-messages")276 v.AddPlayback(pb.Id)277 }278 v.CurrentFolder = curFolder279}280func (v *vmMainInternal) PlayFolder(a *ari.AppInstance, folder string) {281 var pb *ari.Playback282 switch folder {283 case "New":284 pb, _ = a.ChannelsPlay(v.ChannelID, "sound:vm-INBOX")285 case "Old":286 pb, _ = a.ChannelsPlay(v.ChannelID, "sound:vm-Old")287 case "Work":288 pb, _ = a.ChannelsPlay(v.ChannelID, "sound:vm-Work")289 }290 v.AddPlayback(pb.Id)291}292func vmMainMenuIntro(a *ari.AppInstance, vmMainState *vmMainInternal) (vmMainStateFunc, *vmMainInternal) {293 var pb *ari.Playback294 vmMainState.PlayMessageCounts(a)295 fmt.Printf("Message counts are New: %d Old: %d\n", vmMainState.NewMessageCount, vmMainState.OldMessageCount)296 if (vmMainState.NewMessageCount > 0 || vmMainState.OldMessageCount > 0) {297 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-onefor")298 vmMainState.AddPlayback(pb.Id)299 }300 if vmMainState.NewMessageCount > 0 {301 vmMainState.PlayFolder(a, "New")302 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-messages")303 vmMainState.AddPlayback(pb.Id)304 } else if vmMainState.OldMessageCount > 0 {305 vmMainState.PlayFolder(a, "Old")306 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-messages")307 vmMainState.AddPlayback(pb.Id)308 }309 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-opts")310 vmMainState.AddPlayback(pb.Id)311 return vmMainMenu, vmMainState312}313func vmMainMenu(a *ari.AppInstance, vmMainState *vmMainInternal) (vmMainStateFunc, *vmMainInternal) {314 fmt.Println("Entered Main Menu")315 select {316 case event := <-a.Events:317 fmt.Printf("Event type is %s\n", event.Type)318 switch event.Type {319 case "ChannelDtmfReceived":320 vmMainState.PlaybacksStop(a)321 var c ari.ChannelDtmfReceived322 json.Unmarshal([]byte(event.ARI_Body), &c)323 fmt.Printf("Received digit %s\n", c.Digit)324 switch c.Digit {325 case "1":326 case "2":327 return changeFoldersIntro, vmMainState328 case "3":329 return advancedOptionsIntro, vmMainState330 case "0":331 case "*":332 pb, _ := a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-opts")333 vmMainState.AddPlayback(pb.Id)334 case "#":335 return hangupVMMain, vmMainState336 }337 case "PlaybackFinished":338 var p ari.PlaybackFinished339 json.Unmarshal([]byte(event.ARI_Body), &p)340 vmMainState.RemovePlayback(p.Playback.Id)341 return vmMainMenu, vmMainState342 }343 }344 return vmMainMenu, vmMainState345}346func advancedOptionsIntro(a *ari.AppInstance, vmMainState *vmMainInternal) (vmMainStateFunc, *vmMainInternal) {347 pb, _ := a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-tomakecall")348 vmMainState.AddPlayback(pb.Id)349 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-leavemsg")350 vmMainState.AddPlayback(pb.Id)351 return advancedOptions, vmMainState352}353func advancedOptions(a *ari.AppInstance, vmMainState *vmMainInternal) (vmMainStateFunc, *vmMainInternal) {354 select {355 case event := <-a.Events:356 switch event.Type {357 case "ChannelDtmfReceived":358 vmMainState.PlaybacksStop(a)359 var c ari.ChannelDtmfReceived360 json.Unmarshal([]byte(event.ARI_Body), &c)361 switch c.Digit {362 case "4":363 pb, _ := a.ChannelsPlay(vmMainState.ChannelID, "sound:invalid")364 vmMainState.AddPlayback(pb.Id)365 case "5":366 pb, _ := a.ChannelsPlay(vmMainState.ChannelID, "sound:invalid")367 vmMainState.AddPlayback(pb.Id)368 case "*":369 return vmMainMenuIntro, vmMainState370 }371 case "PlaybackFinished":372 var p ari.PlaybackFinished373 json.Unmarshal([]byte(event.ARI_Body), &p)374 vmMainState.RemovePlayback(p.Playback.Id)375 }376 }377 return advancedOptions, vmMainState378}379func changeFoldersIntro(a *ari.AppInstance, vmMainState *vmMainInternal) (vmMainStateFunc, *vmMainInternal) {380 pb, _ := a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-changeto")381 vmMainState.AddPlayback(pb.Id)382 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-press")383 vmMainState.AddPlayback(pb.Id)384 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:digits/0")385 vmMainState.AddPlayback(pb.Id)386 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-for")387 vmMainState.AddPlayback(pb.Id)388 vmMainState.PlayFolder(a, "New")389 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-messages")390 vmMainState.AddPlayback(pb.Id)391 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-press")392 vmMainState.AddPlayback(pb.Id)393 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:digits/1")394 vmMainState.AddPlayback(pb.Id)395 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-for")396 vmMainState.AddPlayback(pb.Id)397 vmMainState.PlayFolder(a, "Old")398 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-messages")399 vmMainState.AddPlayback(pb.Id)400 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-press")401 vmMainState.AddPlayback(pb.Id)402 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:digits/2")403 vmMainState.AddPlayback(pb.Id)404 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-for")405 vmMainState.AddPlayback(pb.Id)406 vmMainState.PlayFolder(a, "Work")407 pb, _ = a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-messages")408 vmMainState.AddPlayback(pb.Id)409 return changeFolders, vmMainState410}411func changeFolders(a *ari.AppInstance, vmMainState *vmMainInternal) (vmMainStateFunc, *vmMainInternal) {412 select {413 case event := <-a.Events:414 switch event.Type {415 case "ChannelDtmfReceived":416 vmMainState.PlaybacksStop(a)417 var c ari.ChannelDtmfReceived418 json.Unmarshal([]byte(event.ARI_Body), &c)419 switch c.Digit {420 case "0":421 vmMainState.CurrentFolder = "New"422 vmMainState.PlayFolder(a, "New")423 pb, _ := a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-messages")424 vmMainState.AddPlayback(pb.Id)425 return vmMainMenuIntro, vmMainState426 case "1":427 vmMainState.CurrentFolder = "Old"428 vmMainState.PlayFolder(a, "Old")429 pb, _ := a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-messages")430 vmMainState.AddPlayback(pb.Id)431 return vmMainMenuIntro, vmMainState432 case "2":433 vmMainState.CurrentFolder = "Work"434 vmMainState.PlayFolder(a, "Work")435 pb, _ := a.ChannelsPlay(vmMainState.ChannelID, "sound:vm-messages")436 vmMainState.AddPlayback(pb.Id)437 return vmMainMenuIntro, vmMainState438 case "*":439 return changeFoldersIntro, vmMainState440 case "#":441 return vmMainMenuIntro, vmMainState442 }443 case "PlaybackFinished":444 var p ari.PlaybackFinished445 json.Unmarshal([]byte(event.ARI_Body), &p)446 vmMainState.RemovePlayback(p.Playback.Id)447 }448 }449 return changeFolders, vmMainState450}451func getMessageCount(mailbox, domain, folder string) int {452 var count int453 db.Ping()454 err := getMessCount.QueryRow(mailbox, domain, folder).Scan(&count)455 if err != nil {456 fmt.Println(err)457 return 0458 }459 return count460}461func authorizeUser(mailbox, domain, password string) bool {462 var mbx string463 db.Ping()464 rows, err :=checkPass.Query(mailbox, domain, password)465 if err != nil {466 fmt.Println(err)467 return false468 }...

Full Screen

Full Screen

vmemory.go

Source:vmemory.go Github

copy

Full Screen

1package memory2import (3 "errors"4 "github.com/uliseslugo/ilusa_pp/types"5)6// Virtual memory struct with address counters & constants map7// TODO: esto no se como lo vamos a usar8type VirtualMemory struct {9 // global segment10 global_int_count int11 global_float_count int12 global_char_count int13 global_bool_count int14 global_ids_count int15 // global temporal segment16 global_temp_int_count int17 global_temp_float_count int18 global_temp_char_count int19 global_temp_bool_count int20 // global_temp_ids_count int21 // local segment22 local_int_count int23 local_float_count int24 local_char_count int25 local_bool_count int26 local_ids_count int27 // local temporal segment28 local_temp_int_count int29 local_temp_float_count int30 local_temp_char_count int31 local_temp_bool_count int32 // local_temp_ids_count int33 // constants segment34 const_int_count int35 const_float_count int36 const_char_count int37 //const_bool_count int38 const_ids_count int39 // pointers40 pointers_int_count int41 pointers_float_count int42 pointers_char_count int43 pointers_bool_count int44 // pointers_ids_count int45 constants_map map[string]int // address for constants map46}47/**48 ResetLocalMemory49 resets counters of local scope in Virtual memory50**/51func (vm *VirtualMemory) ResetLocalMemory() {52 vm.local_int_count = 053 vm.local_char_count = 054 vm.local_bool_count = 055 vm.local_ids_count = 056 vm.local_temp_int_count = 057 vm.local_temp_char_count = 058 vm.local_temp_bool_count = 059 // vm.local_temp_ids_count = 060}61/**62 NewVirtualMemory63 returns virtual memory struct64**/65func NewVirtualMemory() *VirtualMemory {66 return &VirtualMemory{67 0, // vm counters68 0,69 0,70 0,71 0,72 0,73 0,74 0,75 0,76 0,77 0,78 0,79 0,80 0,81 0,82 0,83 0,84 0,85 0,86 0,87 0,88 0,89 0,90 0,91 0,92 0,93 make(map[string]int), // constants map94 }95}96// Functions for next available address97/**98 NextGlobal99 @param t type of global variable100 returns next available address for variable101**/102func (vm *VirtualMemory) NextGlobal(t types.CoreType) (Address, error) {103 switch t {104 case 0: // integer constant105 if vm.global_int_count >= segmentSize {106 return Address(-1), errors.New("Too many integers in global memory.")107 }108 addr := vm.global_int_count + IntOffset + GlobalContext109 // increment counter110 vm.global_int_count++111 return Address(addr), nil112 case 1: // float constant113 if vm.global_float_count >= segmentSize {114 return Address(-1), errors.New("Too many global floats.")115 }116 addr := vm.global_float_count + FloatOffset + GlobalContext117 // increment counter118 vm.global_float_count++119 return Address(addr), nil120 case 2: // chars121 if vm.global_char_count >= segmentSize {122 return Address(-1), errors.New("Too many chars in global memory.")123 }124 addr := vm.global_char_count + CharOffset + GlobalContext125 vm.global_char_count++126 return Address(addr), nil127 case 3: // booleans128 if vm.global_bool_count >= segmentSize {129 return Address(-1), errors.New("Too many booleans in global memory.")130 }131 addr := vm.global_bool_count + BoolOffset + GlobalContext132 vm.global_bool_count++133 return Address(addr), nil134 case 4: // ids135 if vm.global_ids_count >= segmentSize {136 return Address(-1), errors.New("Too many ids in global memory.")137 }138 addr := vm.global_ids_count + IdOffset + GlobalContext139 vm.global_ids_count++140 return Address(addr), nil141 }142 // type mismatch143 return Address(-1), errors.New("Type Error: Type not defined.")144}145/**146 NextGlobalTemp147 @param t type of global variable148 returns next available address for global temporal149**/150func (vm *VirtualMemory) NextGlobalTemp(t types.CoreType) (Address, error) {151 switch t {152 case 0: // integer constant153 if vm.global_temp_int_count >= segmentSize {154 return Address(-1), errors.New("Too many temporal integers in global memory.")155 }156 addr := vm.global_temp_int_count + TempIntOffset + GlobalContext157 // increment counter158 vm.global_temp_int_count++159 return Address(addr), nil160 case 1: // float constant161 if vm.global_temp_float_count >= segmentSize {162 return Address(-1), errors.New("Too many temporal float constants.")163 }164 addr := vm.global_temp_float_count + TempFloatOffset + GlobalContext165 // increment counter166 vm.global_temp_float_count++167 return Address(addr), nil168 case 2: // chars169 if vm.global_temp_char_count >= segmentSize {170 return Address(-1), errors.New("Too many temporal chars in local memory.")171 }172 addr := vm.global_temp_char_count + TempCharOffset + GlobalContext173 vm.global_temp_char_count++174 return Address(addr), nil175 case 3: // booleans176 if vm.global_temp_bool_count >= segmentSize {177 return Address(-1), errors.New("Too many temporal booleans in local memory.")178 }179 addr := vm.global_temp_bool_count + TempBoolOffset + GlobalContext180 vm.global_temp_bool_count++181 return Address(addr), nil182 }183 // type mismatch184 return Address(-1), errors.New("Type Error: Temporal type not defined.")185}186/**187 NextLocal188 @param t type of local variable189 returns next available address for variable190**/191func (vm *VirtualMemory) NextLocal(t types.CoreType) (Address, error) {192 switch t {193 case 0: // integer constant194 if vm.local_int_count >= segmentSize {195 return Address(-1), errors.New("Too many integers in local memory.")196 }197 addr := vm.local_int_count + IntOffset + LocalContext198 // increment counter199 vm.local_int_count++200 return Address(addr), nil201 case 1: // float constant202 if vm.local_float_count >= segmentSize {203 return Address(-1), errors.New("Too many float constants.")204 }205 addr := vm.local_float_count + FloatOffset + LocalContext206 // increment counter207 vm.local_float_count++208 return Address(addr), nil209 case 2: // chars210 if vm.local_char_count >= segmentSize {211 return Address(-1), errors.New("Too many chars in local memory.")212 }213 addr := vm.local_char_count + CharOffset + LocalContext214 vm.local_char_count++215 return Address(addr), nil216 case 3: // booleans217 if vm.local_bool_count >= segmentSize {218 return Address(-1), errors.New("Too many booleans in local memory.")219 }220 addr := vm.local_bool_count + BoolOffset + LocalContext221 vm.local_bool_count++222 return Address(addr), nil223 case 4: // ids224 if vm.local_ids_count >= segmentSize {225 return Address(-1), errors.New("Too many ids in local memory.")226 }227 addr := vm.local_ids_count + IdOffset + LocalContext228 vm.local_ids_count++229 return Address(addr), nil230 }231 // type mismatch232 return Address(-1), errors.New("Type Error: Constant type not defined.")233}234/**235 NextLocalTemp236 @param t type of local variable237 returns next available address for variable238**/239func (vm *VirtualMemory) NextLocalTemp(t types.CoreType) (Address, error) {240 switch t {241 case 0: // integer constant242 if vm.local_temp_int_count >= segmentSize {243 return Address(-1), errors.New("Too many temporal integers in local memory.")244 }245 addr := vm.local_temp_int_count + TempIntOffset + LocalContext246 // increment counter247 vm.local_temp_int_count++248 return Address(addr), nil249 case 1: // float constant250 if vm.local_temp_float_count >= segmentSize {251 return Address(-1), errors.New("Too many temporal float constants.")252 }253 addr := vm.local_temp_float_count + TempFloatOffset + LocalContext254 // increment counter255 vm.local_temp_float_count++256 return Address(addr), nil257 case 2: // chars258 if vm.local_temp_char_count >= segmentSize {259 return Address(-1), errors.New("Too many temporal chars in local memory.")260 }261 addr := vm.local_temp_char_count + TempCharOffset + LocalContext262 vm.local_temp_char_count++263 return Address(addr), nil264 case 3: // booleans265 if vm.local_temp_bool_count >= segmentSize {266 return Address(-1), errors.New("Too many temporal booleans in local memory.")267 }268 addr := vm.local_temp_bool_count + TempBoolOffset + LocalContext269 vm.local_temp_bool_count++270 return Address(addr), nil271 }272 // type mismatch273 return Address(-1), errors.New("Type Error: Temporal type not defined.")274}275/**276 NextConst277 @param t type of constant to be added278 returns next available address of new constant in map279**/280func (vm *VirtualMemory) NextConst(t types.CoreType) (Address, error) {281 switch t {282 case 0: // integer constant283 if vm.const_int_count >= segmentSize {284 return Address(-1), errors.New("Too many integer constants.")285 }286 addr := vm.const_int_count + IntOffset + ConstantsContext287 vm.const_int_count++ // increment counter288 return Address(addr), nil289 case 1: // float constant290 if vm.const_float_count >= segmentSize {291 return Address(-1), errors.New("Too many float constants.")292 }293 addr := vm.const_float_count + FloatOffset + ConstantsContext294 vm.const_float_count++295 return Address(addr), nil296 case 2: // char constant297 if vm.const_char_count >= segmentSize {298 return Address(-1), errors.New("Too many char constants.")299 }300 addr := vm.const_char_count + CharOffset + ConstantsContext301 vm.const_char_count++302 return Address(addr), nil303 // TODO: case 3 - bools304 case 4:305 if vm.const_ids_count >= segmentSize {306 return Address(-1), errors.New("Too many ids constants.")307 }308 addr := vm.const_ids_count + IdOffset + ConstantsContext309 vm.const_ids_count++310 return Address(addr), nil311 }312 return Address(-1), errors.New("Type Error: Constant type not defined.")313}314/**315 NextPointer316 @param t type of constant to be added317 returns next available address of new constant in map318**/319func (vm *VirtualMemory) NextPointer(t types.CoreType) (Address, error) {320 switch t {321 case 0: // integer constant322 if vm.pointers_int_count >= segmentSize {323 return Address(-1), errors.New("Too many integer pointers.")324 }325 addr := vm.pointers_int_count + IntOffset + PointersContext326 vm.pointers_int_count++ // increment counter327 return Address(addr), nil328 case 1: // float constant329 if vm.pointers_float_count >= segmentSize {330 return Address(-1), errors.New("Too many float pointers.")331 }332 addr := vm.pointers_float_count + FloatOffset + PointersContext333 vm.pointers_float_count++ // increment counter334 return Address(addr), nil335 case 2: // char constant336 if vm.pointers_char_count >= segmentSize {337 return Address(-1), errors.New("Too many char pointers.")338 }339 addr := vm.pointers_char_count + CharOffset + PointersContext340 vm.pointers_char_count++ // increment counter341 return Address(addr), nil342 case 3: // bool constant343 if vm.pointers_bool_count >= segmentSize {344 return Address(-1), errors.New("Too many boolean pointers.")345 }346 addr := vm.pointers_bool_count + BoolOffset + PointersContext347 vm.pointers_bool_count++ // increment counter348 return Address(addr), nil349 }350 return Address(-1), errors.New("Type Error: Constant type not defined.")351}...

Full Screen

Full Screen

Count

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(vm.Count(1, 2, 3))4}5import (6func main() {7 fmt.Println(vm.Count(1, 2, 3))8}9import (10func main() {11 fmt.Println(vm.Count(1, 2, 3))12}13import (14func main() {15 fmt.Println(vm.Count(1, 2, 3))16}17import (18func main() {19 fmt.Println(vm.Count(1, 2, 3))20}21import (22func main() {23 fmt.Println(vm.Count(1, 2, 3))24}25import (26func main() {27 fmt.Println(vm.Count(1, 2, 3))28}29import (30func main() {31 fmt.Println(vm.Count(1, 2, 3))32}33import (34func main() {35 fmt.Println(vm.Count(1, 2, 3))36}37import (38func main() {39 fmt.Println(vm.Count(1, 2, 3))40}41import (42func main() {

Full Screen

Full Screen

Count

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(vm.Count(2, 3))4}5import (6func main() {7 fmt.Println(vm.Count(2, 3))8}9import (10func main() {11 fmt.Println(vm.Count(2, 3))12}13import (14func main() {15 fmt.Println(vm.Count(2, 3))16}17import (18func main() {19 fmt.Println(vm.Count(2, 3))20}21import (22func main() {23 fmt.Println(vm.Count(2, 3))24}25import (26func main() {27 fmt.Println(vm.Count(2, 3))28}29import (30func main() {31 fmt.Println(vm.Count(2, 3))32}33import (34func main() {35 fmt.Println(vm.Count(2, 3))36}37import (38func main() {39 fmt.Println(vm.Count(2, 3))40}41import (42func main() {43 fmt.Println(vm.Count(2, 3))44}

Full Screen

Full Screen

Count

Using AI Code Generation

copy

Full Screen

1import (2func main() {3vm1 := vm.New()4vm1.Count()5fmt.Println(vm1)6}7import (8func main() {9vm1 := vm.New()10vm1.Count()11fmt.Println(vm1)12}13import (14func main() {15vm1 := vm.New()16vm1.Count()17fmt.Println(vm1)18}19import (20func main() {21vm1 := vm.New()22vm1.Count()23fmt.Println(vm1)24}25import (26func main() {27vm1 := vm.New()28vm1.Count()29fmt.Println(vm1)30}31import (32func main() {33vm1 := vm.New()34vm1.Count()35fmt.Println(vm1)36}37import (38func main() {39vm1 := vm.New()40vm1.Count()41fmt.Println(vm1)42}43import (44func main() {45vm1 := vm.New()46vm1.Count()47fmt.Println(vm1)48}49import (50func main() {51vm1 := vm.New()52vm1.Count()53fmt.Println(vm1)54}55import (56func main() {57vm1 := vm.New()58vm1.Count()59fmt.Println(vm1)60}61import (62func main() {

Full Screen

Full Screen

Count

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v := vm.New()4 fmt.Println("count:", v.Count())5}6import (7func main() {8 v := vm.New()9 fmt.Println("count:", v.Count())10}11import (12func main() {13 v := vm.New()14 fmt.Println("count:", v.Count())15}16import (17func main() {18 v := vm.New()19 fmt.Println("count:", v.Count())20}21import (22func main() {23 v := vm.New()24 fmt.Println("count:", v.Count())25}26import (27func main() {28 v := vm.New()29 fmt.Println("count:", v.Count())30}31import (32func main() {33 v := vm.New()34 fmt.Println("count:", v.Count())35}36import (37func main() {38 v := vm.New()39 fmt.Println("count:", v.Count())40}41import (

Full Screen

Full Screen

Count

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 v := vm.NewVM()4 fmt.Println(v.Count())5}6Please read [CONTRIBUTING.md](

Full Screen

Full Screen

Count

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 vm := VM.NewVM()4 fmt.Println(vm.Count())5}6import (7func main() {8 vm := VM.NewVM()9 vm.Create("Rohit", 1)10 fmt.Println(vm.Count())11}12import (13func main() {14 vm := VM.NewVM()15 vm.Create("Rohit", 1)16 vm.Start(1)17 fmt.Println(vm.Count())18}19import (20func main() {21 vm := VM.NewVM()22 vm.Create("Rohit", 1)23 vm.Start(1)24 vm.Stop(1)25 fmt.Println(vm.Count())26}27import (28func main() {29 vm := VM.NewVM()30 vm.Create("Rohit", 1)31 vm.Start(1)32 vm.Stop(1)33 vm.Delete(1)34 fmt.Println(vm.Count())35}36import (37func main() {38 vm := VM.NewVM()39 vm.Create("Rohit", 1)40 vm.Start(1)41 vm.Stop(1)42 vm.Delete(1)43 vm.Restart(1)44 fmt.Println(vm.Count())45}46import (47func main() {48 vm := VM.NewVM()

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful