Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(6)

Delta Between Two Patch Sets: state/apiserver/client/client_test.go

Issue 53210044: state;apiserver: Fix a race - lp bug #1067979 (Closed)
Left Patch Set: state;apiserver: Fix a race - lp bug #1067979 Created 11 years, 1 month ago
Right Patch Set: state;apiserver: Fix a race - lp bug #1067979 Created 11 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « state/apiserver/client/client.go ('k') | state/apiserver/export_test.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2012, 2013 Canonical Ltd. 1 // Copyright 2012, 2013 Canonical Ltd.
2 // Licensed under the AGPLv3, see LICENCE file for details. 2 // Licensed under the AGPLv3, see LICENCE file for details.
3 3
4 package client_test 4 package client_test
5 5
6 import ( 6 import (
7 "fmt" 7 "fmt"
8 "net/url" 8 "net/url"
9 "strconv" 9 "strconv"
10 "strings" 10 "strings"
(...skipping 1657 matching lines...) Expand 10 before | Expand all | Expand 10 after
1668 Nonce: "foo", 1668 Nonce: "foo",
1669 HardwareCharacteristics: instance.MustParseHardware("arch=amd64" ), 1669 HardwareCharacteristics: instance.MustParseHardware("arch=amd64" ),
1670 } 1670 }
1671 machines, err := s.APIState.Client().AddMachines([]params.AddMachinePara ms{apiParams}) 1671 machines, err := s.APIState.Client().AddMachines([]params.AddMachinePara ms{apiParams})
1672 c.Assert(err, gc.IsNil) 1672 c.Assert(err, gc.IsNil)
1673 c.Assert(len(machines), gc.Equals, 1) 1673 c.Assert(len(machines), gc.Equals, 1)
1674 machineId := machines[0].Machine 1674 machineId := machines[0].Machine
1675 // Call ProvisioningScript. Normally ProvisioningScript and 1675 // Call ProvisioningScript. Normally ProvisioningScript and
1676 // MachineConfig are mutually exclusive; both of them will 1676 // MachineConfig are mutually exclusive; both of them will
1677 // allocate a state/api password for the machine agent. 1677 // allocate a state/api password for the machine agent.
1678 » script, err := s.APIState.Client().ProvisioningScript(machineId, apiPara ms.Nonce) 1678 » script, err := s.APIState.Client().ProvisioningScript(params.Provisionin gScriptParams{
1679 » » MachineId: machineId,
1680 » » Nonce: apiParams.Nonce,
1681 » })
1679 c.Assert(err, gc.IsNil) 1682 c.Assert(err, gc.IsNil)
1680 mcfg, err := statecmd.MachineConfig(s.State, machineId, apiParams.Nonce, "") 1683 mcfg, err := statecmd.MachineConfig(s.State, machineId, apiParams.Nonce, "")
1681 c.Assert(err, gc.IsNil) 1684 c.Assert(err, gc.IsNil)
1682 cloudcfg := coreCloudinit.New() 1685 cloudcfg := coreCloudinit.New()
1683 err = cloudinit.ConfigureJuju(mcfg, cloudcfg) 1686 err = cloudinit.ConfigureJuju(mcfg, cloudcfg)
1684 c.Assert(err, gc.IsNil) 1687 c.Assert(err, gc.IsNil)
1685 cloudcfg.SetAptUpgrade(false) 1688 cloudcfg.SetAptUpgrade(false)
1686 sshinitScript, err := sshinit.ConfigureScript(cloudcfg) 1689 sshinitScript, err := sshinit.ConfigureScript(cloudcfg)
1687 c.Assert(err, gc.IsNil) 1690 c.Assert(err, gc.IsNil)
1688 // ProvisioningScript internally calls MachineConfig, 1691 // ProvisioningScript internally calls MachineConfig,
1689 // which allocates a new, random password. Everything 1692 // which allocates a new, random password. Everything
1690 // about the scripts should be the same other than 1693 // about the scripts should be the same other than
1691 // the line containing "oldpassword" from agent.conf. 1694 // the line containing "oldpassword" from agent.conf.
1692 scriptLines := strings.Split(script, "\n") 1695 scriptLines := strings.Split(script, "\n")
1693 sshinitScriptLines := strings.Split(sshinitScript, "\n") 1696 sshinitScriptLines := strings.Split(sshinitScript, "\n")
1694 c.Assert(scriptLines, gc.HasLen, len(sshinitScriptLines)) 1697 c.Assert(scriptLines, gc.HasLen, len(sshinitScriptLines))
1695 for i, line := range scriptLines { 1698 for i, line := range scriptLines {
1696 if strings.Contains(line, "oldpassword") { 1699 if strings.Contains(line, "oldpassword") {
1697 continue 1700 continue
1698 } 1701 }
1699 c.Assert(line, gc.Equals, sshinitScriptLines[i]) 1702 c.Assert(line, gc.Equals, sshinitScriptLines[i])
1700 } 1703 }
1701 } 1704 }
1702 1705
1706 func (s *clientSuite) TestProvisioningScriptDisablePackageCommands(c *gc.C) {
1707 apiParams := params.AddMachineParams{
1708 Jobs: []params.MachineJob{params.JobHostUnits},
1709 InstanceId: instance.Id("1234"),
1710 Nonce: "foo",
1711 HardwareCharacteristics: instance.MustParseHardware("arch=amd64" ),
1712 }
1713 machines, err := s.APIState.Client().AddMachines([]params.AddMachinePara ms{apiParams})
1714 c.Assert(err, gc.IsNil)
1715 c.Assert(len(machines), gc.Equals, 1)
1716 machineId := machines[0].Machine
1717 for _, disable := range []bool{false, true} {
1718 script, err := s.APIState.Client().ProvisioningScript(params.Pro visioningScriptParams{
1719 MachineId: machineId,
1720 Nonce: apiParams.Nonce,
1721 DisablePackageCommands: disable,
1722 })
1723 c.Assert(err, gc.IsNil)
1724 var checker gc.Checker = jc.Contains
1725 if disable {
1726 // We disabled package commands: there should be no "apt " commands in the script.
1727 checker = gc.Not(checker)
1728 }
1729 c.Assert(script, checker, "apt-get")
1730 }
1731 }
1732
1703 func (s *clientSuite) TestClientAuthorizeStoreOnDeployServiceSetCharmAndAddCharm (c *gc.C) { 1733 func (s *clientSuite) TestClientAuthorizeStoreOnDeployServiceSetCharmAndAddCharm (c *gc.C) {
1704 store, restore := makeMockCharmStore() 1734 store, restore := makeMockCharmStore()
1705 defer restore() 1735 defer restore()
1706 1736
1707 oldConfig, err := s.State.EnvironConfig() 1737 oldConfig, err := s.State.EnvironConfig()
1708 c.Assert(err, gc.IsNil) 1738 c.Assert(err, gc.IsNil)
1709 1739
1710 attrs := coretesting.Attrs(oldConfig.AllAttrs()) 1740 attrs := coretesting.Attrs(oldConfig.AllAttrs())
1711 attrs = attrs.Merge(coretesting.Attrs{"charm-store-auth": "token=value"} ) 1741 attrs = attrs.Merge(coretesting.Attrs{"charm-store-auth": "token=value"} )
1712 1742
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1756 c.Assert(err, gc.ErrorMatches, "charm URL must include revision") 1786 c.Assert(err, gc.ErrorMatches, "charm URL must include revision")
1757 1787
1758 // Add a charm, without uploading it to storage, to 1788 // Add a charm, without uploading it to storage, to
1759 // check that AddCharm does not try to do it. 1789 // check that AddCharm does not try to do it.
1760 charmDir := coretesting.Charms.Dir("dummy") 1790 charmDir := coretesting.Charms.Dir("dummy")
1761 ident := fmt.Sprintf("%s-%d", charmDir.Meta().Name, charmDir.Revision()) 1791 ident := fmt.Sprintf("%s-%d", charmDir.Meta().Name, charmDir.Revision())
1762 curl := charm.MustParseURL("cs:quantal/" + ident) 1792 curl := charm.MustParseURL("cs:quantal/" + ident)
1763 bundleURL, err := url.Parse("http://bundles.testing.invalid/" + ident) 1793 bundleURL, err := url.Parse("http://bundles.testing.invalid/" + ident)
1764 c.Assert(err, gc.IsNil) 1794 c.Assert(err, gc.IsNil)
1765 sch, err := s.State.AddCharm(charmDir, curl, bundleURL, ident+"-sha256") 1795 sch, err := s.State.AddCharm(charmDir, curl, bundleURL, ident+"-sha256")
1796 c.Assert(err, gc.IsNil)
1766 1797
1767 name := charm.Quote(sch.URL().String()) 1798 name := charm.Quote(sch.URL().String())
1768 storage := s.Conn.Environ.Storage() 1799 storage := s.Conn.Environ.Storage()
1769 _, err = storage.Get(name) 1800 _, err = storage.Get(name)
1770 c.Assert(err, jc.Satisfies, errors.IsNotFoundError) 1801 c.Assert(err, jc.Satisfies, errors.IsNotFoundError)
1771 1802
1772 // AddCharm should see the charm in state and not upload it. 1803 // AddCharm should see the charm in state and not upload it.
1773 err = client.AddCharm(sch.URL()) 1804 err = client.AddCharm(sch.URL())
1774 c.Assert(err, gc.IsNil) 1805 c.Assert(err, gc.IsNil)
1775 _, err = storage.Get(name) 1806 _, err = storage.Get(name)
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1825 // contains the correct data. 1856 // contains the correct data.
1826 sch, err := s.State.Charm(curl) 1857 sch, err := s.State.Charm(curl)
1827 c.Assert(err, gc.IsNil) 1858 c.Assert(err, gc.IsNil)
1828 storage, err := envtesting.GetEnvironStorage(s.State) 1859 storage, err := envtesting.GetEnvironStorage(s.State)
1829 c.Assert(err, gc.IsNil) 1860 c.Assert(err, gc.IsNil)
1830 uploads, err := storage.List(fmt.Sprintf("%s-%d-", curl.Name, curl.Revis ion)) 1861 uploads, err := storage.List(fmt.Sprintf("%s-%d-", curl.Name, curl.Revis ion))
1831 c.Assert(err, gc.IsNil) 1862 c.Assert(err, gc.IsNil)
1832 c.Assert(uploads, gc.HasLen, 1) 1863 c.Assert(uploads, gc.HasLen, 1)
1833 c.Assert(getArchiveName(sch.BundleURL()), gc.Equals, uploads[0]) 1864 c.Assert(getArchiveName(sch.BundleURL()), gc.Equals, uploads[0])
1834 s.assertUploaded(c, storage, sch.BundleURL(), sch.BundleSha256()) 1865 s.assertUploaded(c, storage, sch.BundleURL(), sch.BundleSha256())
1866 }
1867
1868 func (s *clientSuite) TestAddCharmOverwritesPlaceholders(c *gc.C) {
1869 store, restore := makeMockCharmStore()
1870 defer restore()
1871
1872 client := s.APIState.Client()
1873 curl, _ := addCharm(c, store, "wordpress")
1874
1875 // Add a placeholder with the same charm URL.
1876 err := s.State.AddStoreCharmPlaceholder(curl)
1877 c.Assert(err, gc.IsNil)
1878 _, err = s.State.Charm(curl)
1879 c.Assert(err, jc.Satisfies, errors.IsNotFoundError)
1880
1881 // Now try to add the charm, which will convert the placeholder to
1882 // a pending charm.
1883 err = client.AddCharm(curl)
1884 c.Assert(err, gc.IsNil)
1885
1886 // Make sure the document's flags were reset as expected.
1887 sch, err := s.State.Charm(curl)
1888 c.Assert(err, gc.IsNil)
1889 c.Assert(sch.URL(), jc.DeepEquals, curl)
1890 c.Assert(sch.IsPlaceholder(), jc.IsFalse)
1891 c.Assert(sch.IsUploaded(), jc.IsTrue)
1892 }
1893
1894 func (s *clientSuite) TestCharmArchiveName(c *gc.C) {
1895 for rev, name := range []string{"Foo", "bar", "wordpress", "mysql"} {
1896 archiveFormat := fmt.Sprintf("%s-%d-[0-9a-f-]+", name, rev)
1897 archiveName, err := client.CharmArchiveName(name, rev)
1898 c.Check(err, gc.IsNil)
1899 c.Check(archiveName, gc.Matches, archiveFormat)
1900 }
1835 } 1901 }
1836 1902
1837 func (s *clientSuite) assertPutCalled(c *gc.C, ops chan dummy.Operation, numCall s int) { 1903 func (s *clientSuite) assertPutCalled(c *gc.C, ops chan dummy.Operation, numCall s int) {
1838 calls := 0 1904 calls := 0
1839 select { 1905 select {
1840 case op, ok := <-ops: 1906 case op, ok := <-ops:
1841 if !ok { 1907 if !ok {
1842 return 1908 return
1843 } 1909 }
1844 if op, ok := op.(dummy.OpPutFile); ok { 1910 if op, ok := op.(dummy.OpPutFile); ok {
(...skipping 17 matching lines...) Expand all
1862 c.Assert(err, gc.IsNil) 1928 c.Assert(err, gc.IsNil)
1863 defer reader.Close() 1929 defer reader.Close()
1864 downloadedSHA256, _, err := utils.ReadSHA256(reader) 1930 downloadedSHA256, _, err := utils.ReadSHA256(reader)
1865 c.Assert(err, gc.IsNil) 1931 c.Assert(err, gc.IsNil)
1866 c.Assert(downloadedSHA256, gc.Equals, expectedSHA256) 1932 c.Assert(downloadedSHA256, gc.Equals, expectedSHA256)
1867 } 1933 }
1868 1934
1869 func getArchiveName(bundleURL *url.URL) string { 1935 func getArchiveName(bundleURL *url.URL) string {
1870 return strings.TrimPrefix(bundleURL.RequestURI(), "/dummyenv/private/") 1936 return strings.TrimPrefix(bundleURL.RequestURI(), "/dummyenv/private/")
1871 } 1937 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b